Create package.
json
npm init -y for package.json
index.js is the main file where we write code (server
connect)
run index.js
a. node index.js in terminal
b. run using server . go to package.json -> in “scripts”:
add “serve”: node index.js.
install express framework
npm i express
on successful installation in package.json ->
dependencies it will show the version like this
“express”: version
now copy and run the boilerplate code to see if it works
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
The other way is
const express = require(‘express’)
//this line import the express module and assigns it to variable
‘express’
const app = express()
// create an instance of an express application
app.listen(3000, ()=> {
console.log(‘Server is running on port 300’);
});
// this means server is running on locally on port 3000 if not
console will show error
app.get(‘/’, (req, res) => {
res.send(“Hello from Node API”)
});
// ‘/’ – means the home page,
Req – is the request send by the users
Res – is the response to the request send from the server
Status , size and Time will show in terminal for each serves
const express = require('express'):
This line imports the Express module and assigns it to the variable express.
require('express') is a Node.js function that loads the Express library, which is a
minimal and flexible web application framework that helps you build web servers
and APIs.
const express stores the imported Express object, giving you access to all its
functionality.
const app = express():
This line creates an instance of an Express application.
express() is a function that initializes an Express app.
const app holds this Express application instance, which you can use to define routes,
middleware, and start your server to listen for incoming requests.