This project aims to give you a real-world scenario in which you would read and write to your disk via a Node.js express server rather than a database. The project you create serves two purposes: to prepare you for setting up scalable code and architecture for real-world projects and tie together some of the most popular middleware and utilities found in Node.js projects. This project barely touches the surface of what is possible but will prove your ability to use what youβve learned in real-world scenarios.
For this project, refactor and test as much as possible while you are building. Since you are using TypeScript and an unfamiliar library, it is sometimes easier to write and build in plain JS to see what your functions return; remember your submission needs to be in TypeScript. As your skills improve, typing in TypeScript will feel more intuitive. Make sure to remove any debugging code from your final submission.
##Initialization
npm init -yto start new node project
npm i --save-dev typescriptadd typescript to dev dependenciesnpm i --save-dev ts-nodeadd ts-node to execute ts on nodenpm i --save-dev @types/nodetype definitionsnpx tsc --initadd the default TypeScript configuration file."build": "npx tsc"add a build script to your package.json file- add the following to
tsconfig.json:
"target": "es5",
"lib": ["ES6"],
"outDir": "./build",
"strict": true,
"noImplicitAny": true
"exclude": ["node_modules"]and exclude checking of node modules by adding an "exclude" parameternpm run buildto run build scriptnode dev/.
npm install expressinstall expressnpm i --save-dev @types/expresstype definitions
npm i --save-dev nodemonto restart server after changes- add
"start": "nodemon src/index.ts"script to run nodemon
npm install morganlog server output- include
app.use(morgan("common"))to add logger to js file
npm i --save-dev prettieradd prettier to dev dependencies"prettier": "prettier --config .prettierrc 'src/**/*.js' --write"add prettier script to package.json- create .prettierrc file in root directory
- run script with
npm run prettier
npm i --save-dev eslintadd to dev dependenciesnpm i --save-dev eslint-config-prettiernpm i --save-dev eslint-plugin-prettiernpm i eslint-config-airbnb-base eslint-plugin-importsave airbnb style guide- add
"lint": "eslint 'dist/index.js'"to scripts to run lint on index.js eslint --init- run
npm run lintto run linting
npm i jasmineto install jasminenpm i jasmine-spec-reporterto add a reporter for outputting Jasmine results to the terminalnpm i --save-dev @types/jasmineto add type definitions for Jasmine- BEST PRACTICE:
- When creating files for tests, a best practice is to name the .ts file the same as the .js file to be tested with Spec appended to the end. The more tests needed to be run, the more test files will need to be created. Be sure to follow this best practice to keep track of the test file that contains the tests for each .js file.
- create the file structure noted below
- include
"exclude": ["node_modules", "./dist", "spec"]after compilerOptions in tsconfig.json because we don't want to check these for TS - add this to reporter.ts
import {DisplayProcessor, SpecReporter, StacktraceOption} from "jasmine-spec-reporter";
import SuiteInfo = jasmine.SuiteInfo;
class CustomProcessor extends DisplayProcessor {
public displayJasmineStarted(info: SuiteInfo, log: string): string {
return `${log}`;
}
}
jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: StacktraceOption.NONE
},
customProcessors: [CustomProcessor],
}));
- and this to jasmine.json
{
"spec_dir": "dist/tests",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
- add the following script to build and run at the same time
"test": "npm run build && npm run jasmine"
- https://www.npmjs.com/package/sharp
- https://malcoded.com/posts/nodejs-image-resize-express-sharp/
- The typical use case for this high speed Node.js module is to convert large images in common formats to smaller, web-friendly JPEG, PNG, WebP and AVIF images of varying dimensions.
npm install --save-dev sharpnpm i --save-dev @types/sharp
βββ node_modules
βββ dist
β βββ index.js
β βββ tests
β βββ utilities
βββ spec
β βββ support
β βββ jasmine.json
βββ src
β βββ tests
β β βββ helpers
β β β βββ reporter.ts
β β βββ indexSpec.ts
β β βββ resizeSpec.ts
β βββ utilities
β β βββ resize.ts
β βββ images
β β βββ mario.jpg
β β βββ wario.jpg
β β βββ peach.jpg
β β βββ toad.jpg
β β βββ luigi.jpg
β βββ resizedImages
β β βββ mario-100-100.jpg
β βββ index.ts
βββ package-lock.json
βββ package.json
βββ .prettierrc
βββ .eslintrc.js
βββ README.md
βββ scratch.txt
βββ tsconfig.json
- Install
npm i --save-dev jasminenpm i --save-dev jasmine-spec-reporterjasmine output to console in a readable waynpm i --save-dev @types/jasminedefine type definitions
- Add testing script
"jasmine": "jasmine"
- Set up file structure
- add spec folder at root level
- add subfolder to spec called support
- add jasmine.json file to support folder
- add tests folder in src folder
- add indexSpec.ts file to test main index file
- add helpers folder
- add reporter.ts where Specs look for their configurations
- Configure Jasmine
- use default settings for reporter.ts found at https://github.com/bcaudan/jasmine-spec-reporter/tree/master/examples/typescript
- add the following to jasmine.json to configure jasmine
{ "spec_dir": "dist/tests", "spec_files": [ "**/*[sS]pec.js" ], "helpers": [ "helpers/**/*.js" ], "stopSpecOnExpectationFailure": false, "random": false }- endpoint testing
- install supertest
npm i --save-dev supertestnpm i --save-dev @types/supertest
- add to spec file 1.
- create test
- install supertest
- Create testing script
- Create tests
- install required modules with
npm install - compile and test typescript with
npm run test - run server with
npm run start - go to browser and get endpoint with
http://localhost:3000/resize - How to write browser query commands
- to resize the image, two query arguments are needed: width and height. example below
http://localhost:3000/?filename=imagewidth=100&height=100- '?' indicates query
- specify an image file found in src/images to resize with the query above
- specify arguments with keywords 'width' and 'height'
- string arguments together with '&'
- the result is a new jpg image resized to the specified dimensions
- piped to user
- written to cache (src/resizedImages)
- filename query argument must be found in src/images
- add new images to resize by adding them to src/images