To create and setup node js project with express, simply create a project folder and then run npm install express -save command to install Express JS in it and set up the entry point file such as app.js, server.js or main.js for your express app.
Here are steps to create node js project with express js:
Step 1: Install Node Js
To install and set up Express Project, you need to install Node JS in your system. If you don’t know how to install and setup Node JS, you can read these guides according to your OS system:
- How to Install Node.js and NPM on Windows 11
- How to Install Node js and npm on Ubuntu
- How to Install Node JS on CentOS
Step 2: Create A New Project
Type the mkdir ExpressApp
command on CMD or terminal window to create a new project directory for your Express JS application:
mkdir ExpressApp
To navigate to your expressApp directory by using cd ExpressApp
command:
cd ExpressApp
Step 3: Install Express JS
Simply type npm init -y
command on cmd or terminal and hit enter to set up node js and npm related packages in your express node js application or project:
npm init -y
To install Express JS, just type npm install express -save
command and hit enter to install express js into your project directory:
npm install express -save
Step 4: Setup Express js App
To create the main entry point into your express node js app, simply use touch index.js
or type nul > index.js
on cmd or terminal window to create file:
For ubuntu and mac os::
touch index.js
For windows:
type nul > index.js
After that, add the following lines of code to the app.js file:
const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => res.send('Hello World!')); app.listen(port, () => console.log(`Express app running on port ${port}!`));
Step 5: Test Express js App
Now, run the express node js application or project by using node app.js
or npm start
command on cmd or terminal window:
node index.js or npm start
Now your server is ready, simply open your browser and hit http://localhost:3000
to test your first Express Node JS application.
Conclusion
Congratulations! You have successfully created and set up your first create node js project with express js.