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
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
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}`))
8
1const express = require('express');
2const bodyParser = require('body-parser');
3const mongoose = require('mongoose');
4const cors = require('cors')
5const bcrypt = require('bcrypt')
6const jwt = require('jsonwebtoken')
7
8//connecting to my cluster database online
9mongoose.connect('mongodb+srv://username:password@clustername.elei8.mongodb.net/databasename?retryWrites=true&w=majority', {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
10
11//initializing the app
12const app = express()
13
14//required middlware
15app.use(cors())
16app.use(bodyParser.json())
17app.use(bodyParser.urlencoded({ extended: false }))
18
19//Setting default port for the backend server
20const PORT = process.env.PORT || 3333
21// run in the terminal : export PORT=3333 {{justincase}}
22
23
24//Test server page running
25app.get('/', (req, res) => {
26 res.send(`Hello World ,,,, backend is running on port ${PORT}`)
27})
28
29// Console log for terminal for server listening
30app.listen(PORT, (err) => {
31 if(err) return console.log(err);
32 console.log(`Server listening on port ${PORT}`)
33})
34
1const express = require('express')const app = express() app.get('/', function (req, res) { res.send('Hello World')}) app.listen(3000)
1// create directory
2
3//npm init -y
4//npm i express --save
5
6//create public directory
7//create server.js
8
9// <---- In the server js file --->
10
11'use strict';
12
13const express = require('express');
14const app = express();
15app.use(express.static('public'));// to connect with frontend html
16app.use(express.json());//body parse
17
18app.get('/', function(req,res){
19 res.send('This is the Homepage');
20 //res.sendFile('index.html');
21});
22
23app.listen(3000);
24