1/* ====== create node.js server with express.js framework ====== */
2// dependencies
3const express = require("express");
4
5const app = express();
6
7app.get("/", (req, res) => {
8 res.send("This is home page.");
9});
10
11app.post("/", (req, res) => {
12 res.send("This is home page with post request.");
13});
14
15// PORT
16const PORT = 3000;
17
18app.listen(PORT, () => {
19 console.log(`Server is running on PORT: ${PORT}`);
20});
21
22
23// ======== Instructions ========
24// save this as index.js
25// you have to download and install node.js on your machine
26// open terminal or command prompt
27// type node index.js
28// find your server at http://localhost:3000
1//to run : node filename.js
2const express = require('express')
3const app = express()
4const port = 3000
5
6app.get('/', (req, res) => res.send('Hello World!'))
7
8app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
9
10//visit localhost:3000
11// assuming you have done 1) npm init 2) npm install express
1const express = require('express')
2const app = express()
3const port = 3000
4
5app.get('/', (req, res) => res.send('Hello World!'))
6
7app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
1basic server
2
3const express =require('express');
4const app = express();
5const PORT = 5000;
6
7
8app.get('/',(req,res)=>{
9 res.json({message: 'Welcome to the backend'})
10})
11
12
13app.listen(PORT ,()=>console.log(`Connected to ${PORT}`)
14
15
1
2const express = require('express')
3const app = express()
4const port = 3000
5
6app.get('/', (req, res) => {
7 res.send('Hello World!')
8})
9
10app.listen(port, () => {
11 console.log(`Example app listening at http://localhost:${port}`)
12})
13
1$ npm init --y //add the package.json file
2
3$ npm install express --no-save
4
5const express = require('express')
6const app = express()
7
8//cors to fix cors origin, body-parser to fix the post value on the server
9const cors = require('cors');
10const bodyParser = require('body-parser');
11app.use(cors());
12app.use(bodyParser.json());
13
14const port = 3000
15
16app.get('/', (req, res) => {
17 res.send('Hello World!')
18})
19
20router.get('/admin/:id?', (req, res) => {
21 let id = req.params.id;
22}
23
24app.post('/admin', (req, res) => {
25 const user = req.body.user;
26 res.send('Hello World!', user)
27})
28
29app.listen(port, () => {
30 console.log(`Example app listening at http://localhost:${port}`)
31})