- Initialize a new Node.js project
npm init -y
- Install the required dependencies by running the following command:
npm install express cors @types/express @types/cors ts-node typescript nodemon --save-dev
- Init typescript. Might work without npx
npx tsc --init
- Set tsconfig.json file. This tells TypeScript to transpile your code to ES6 and output the compiled files to the ./dist directory.
{ "compilerOptions": { "target": "es6", "module": "commonjs", "esModuleInterop": true, "outDir": "./dist", "sourceMap": true }, "include": [ "./src/**/*" ] }
- Add .gitignore file, add node_modules and whatever else to it
touch .gitignore
- Create a new src directory in the root of your project, and create a new file called index.ts inside it.
import express, { Application, Request, Response } from 'express'; import cors from 'cors'; const app: Application = express(); const port = 3000; app.use(cors()); app.get('/', (req: Request, res: Response) => { res.send('Hello, world!'); }); app.listen(port, () => { console.log(`Server running on port ${port}`); }); export { app };
- Add a new script to your package.json file to start the server using ts-node and nodemon:
"scripts": { "start": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts" }
- Run the server by executing npm run start in your terminal. You should see the message "Server running on port 3000" in your console.
- Visit http://localhost:3000 in your web browser to see the "Hello, world!" message displayed.
- In src/index.ts add a ping endpoint that accepts a message param and call it via curl
curl http://localhost:3000/ping\?message\=hello
- Install test deps
npm install mocha chai chai-http @types/mocha --save-dev
- Add first test
mkdir test && touch test/endpoint.test.ts
import chai from 'chai'; import chaiHttp from 'chai-http'; import { app } from '../src/index'; chai.use(chaiHttp); describe('Endpoint tests', () => { it('Ping endpoint should return status 200', (done) => { const msg = "hello"; chai.request(app) .get('/ping') .query({ "message": msg }) .end((err, res) => { chai.expect(res).to.have.status(200); chai.expect(res.body).to.have.property("env", "TEST") chai.expect(res.body).to.have.property("version", "1.0.0") done(); }); }); });