Setup Express JS project
- Create a new directory and install the node modules
$ makdir authentication
$ cd authentication
$ npm init -y
2. After that let's add Typescript to the project. we can do that by running the following command.
$ npm i -g typescript
$ npm i -D typescript
3. Add express to the project. we can do that by running the following command.
$ npm i express
$ npm i -D @types/express
4. Open project in visual studio code.Create new folder in project root directory, which name is src.Under src create file index.ts.
// index.ts
import express from 'express';
const app = express();
const PORT = 3000;
app.listen(PORT, ()=>{
console.log(`Running on Port ${PORT});
})
5. Now open terminal and run node project.
$ node ./src/index.ts
When you are run this command, this will giving error
Solve this problem create tsconfig.json. We can do by running following command.
$ npx tsc --init
5. Add some change in tsconfig.json
"rootDir":"./src",
"outDir":"./dist",
"noImplicitAny": true,
tsconfig.json
6. Generate dist folder in root directory. You can generate folder using following command.
$ npx tsc --build
7. Now try to run project using command
$ node ./dist/index.js
Congratulations, i are setup node js project successfully.
8. Add some change in package.json, which help us to real time run project without every time build project then run project for changes reflect.
// package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
"build": "rimraf dist && tsc",
"start:dev": "nodemon ./src/index.ts"
},
9. Install nodemon,
nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.
nodemon does not require any additional changes to your code or method of development. nodemon is a replacement wrapper for node
. To use nodemon
, replace the word node
on the command line when executing your script. you can install node using following command.
$ npm i -D nodemon
10 . Now you can running using following command:
$ npm run start:dev
Login to leave a comment.