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// this is your code
2// ZDev1#4511 on discord if you want more help!
3// first you should install express in the terminal
4// `npm i express`.
5const express = require('express');
6const app = express();
7
8// route
9app.get('/', (req,res)=>{
10 // Sending This is the home page! in the page
11 res.send('This is the home page!');
12});
13
14// Listening to the port
15let PORT = 3000;
16app.listen(PORT)
17
18// FINISH!
1const express = require('express');
2const app = express();
3const PORT = process.env.PORT || 3000;
4
5app.get('/', (req, res) => {
6 res.send('<h1>Some HTML</h1>');
7 res.send('<p>Even more HTML</p>');
8});
9
10app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));
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 app = express();
3const mongoose = require('mongoose');
4const bodyParser = require('body-parser');
5
6app.use(bodyParser.json());
7const PORT = process.env.PORT || 3000;
8
9app.use(bodyParser.json());
10
11//connecting to db
12try {
13 mongoose.connect('mongodb://localhost/YOUR_DB_NAME', {
14 useNewUrlParser: true,
15 useUnifiedTopology: true,
16 useCreateIndex: true,
17 }, () =>
18 console.log("connected"));
19 } catch (error) {
20 console.log("could not connect");
21 }
22
23app.get('/', (req, res) => {
24 res.send('<h1>Some HTML</h1>');
25 res.send('<p>Even more HTML</p>');
26});
27
28
29
30app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));