0% found this document useful (0 votes)
29 views3 pages

Express Js

The document provides instructions for setting up a Node.js application using Express. It includes steps to create a package.json file, install the Express framework, and run a basic server that responds with 'Hello World' or 'Hello from Node API'. Additionally, it explains the purpose of key code components such as importing Express and creating an application instance.

Uploaded by

keqingtake1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Express Js

The document provides instructions for setting up a Node.js application using Express. It includes steps to create a package.json file, install the Express framework, and run a basic server that responds with 'Hello World' or 'Hello from Node API'. Additionally, it explains the purpose of key code components such as importing Express and creating an application instance.

Uploaded by

keqingtake1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

 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.

You might also like