This blog will give you brief information of how to Create server using Node.js, Express. Lets understand and go through every step of code and features.
Setting up Node.js project
Lets start with creating new project directory
Step-I : Creating New Project Directory
Make a new directory for the project. For creating the new directory open terminal at location where we want to create project folder. Now for creating folder use "mkdir" command.
mkdir your-project-name
For opening the new directory use "cd" command as follow
cd your-project-name
Step-II: Establishing Node.js project
For creating the Node.js project we need to initialize it. In order do this use "npm init" command in the directory of our project.
npm init
After using npm init command you will be prompted to enter the information about the project such as Name, Version, Description, Entry Point(Generally it is index.js), and more such options. We can provide our own info setting or else we can accept the default setting by pressing Enter.
Once the above step is completed a package.json file will be created in our project directory. All the information related to Project and Dependencies is included in the package.json file.
Step-III: Setting up the file structure in project
Step-IV: Installing express and dotenv
In our project we need Express to build single page application and web application. Express will provide the REST services like handling routes, requests and responses. Dotenv is basically used for loading the environment variables from a .env file into process.env .It is also used for keeping your sensitive data like database credentials, API keys safe.
For installing above packages use command below:
npm install express dotenv cors
After executing above command node_modules folder will be created in our project along with package-lock.json file.
Before starting to code ensure which type of structure we want to use in package.json file from two options "commonjs" or "module".
In our project we will use module as shown in above image "type":"module"
Also we need to install nodemon dev-dependency. Nodemon is tool which automatically restart server after any update done in our nodejs code.
npm install --save-dev nodemon
After running install command do change "scripts" as shown in image below
Step-V : Creating And Starting Server
Creating The Server
The first we need to do is go to our app.js file and import below packages which are required for creating server and CRUD operations.
//Required packages
import express from 'express' ;
import cors from 'cors';
Now we will create server by using express().If we want to provide CORS_ORIGIN then we can provide it in .env file and then use it in configuration of cors. If we want to provide access for all origin then write CORS_ORIGIN= *
const app = express();
app.use(
cors({
origin: process.env.CORS_ORIGIN,
credentials: true,
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: true, limit: '16kb' }));
app.use(express.static('public'));
export { app };
Now go to index.js file and write below code to start server on the port as defined in .env file or 3000.
import { app } from './app.js';
import dotenv from 'dotenv';
dotenv.config({ path: './env' });
const port=process.env.PORT || 3000;
app.listen(port, () => {
console.log(`server is running on port ${port}`);
});
After adding above code go to the terminal and run below command to start server.
Conclusion:
These are the steps for setting up project in Nodejs and starting backend server.
Top comments (0)