In this tutorial, we will learn, how to install expressjs and start an express server. This tutorial shows you how to use the Express framework and Node.js to get a simple server setting up NodeJS server and running.
Few commands to check nodejs details:
node version
node -v
npm version
npm -v
Set Up Your Project
mkdir express-tutorial
Then Create node express tutorials
npm init -y
The above command will create a package.json file and answer "yes" to all the questions because of the flag.
you will see like this of package.json file:
{
"name": "express-tutorial",
"version": "1.0.0",
"description": "This Tutorial PHP",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Robert Look",
"license": "MIT"
}
In this case if your package.json is not the same, that is completely wrong to set up a node.js server. You can add them to the fields you want to match, but I'll show you which fields to check as this file changes.
Then, we need to set up express js, meanwhile, add to the Express frame. The Node Express course is a framework of Node.js that we will use to create repository APIs, so we will need to install that Express package. To do this use:
npm i express
Running this command will add a few new files:
├── node_modules #new
├── package-lock.json #new
└── package.json
Create Initial Server
First, we want to create a file to store our main server code we learn setting up a node.js server:
touch index.js
It is best to use index.js as a root file as this interacts with other developers who indicate that this is where your application starts in the node.js express course.
here you want to take whatever you named and type:
const express = require("express");
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
To test out the code, go back to the command line and run:
node index.js
If you have followed all the steps so far, you should see the message in your terminal:
Example app listening at http://localhost:3000
I hope it can help you...