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
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
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}`))
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
14
1// node js -> express -> basic example: static folder, 404 page
2
3const express = require('express');
4const path = require('path');
5const PORT = process.env.PORT || 5000;
6
7const app = express();
8
9function error404(req, res) {
10 res.status(404);
11
12 if (req.accepts('html')) {
13 res.sendFile(path.join(__dirname, 'public/errors/404.html'));
14 return;
15 }
16
17 if (req.accepts('json')) {
18 res.send({
19 status: 404,
20 error: 'Not found'
21 });
22 return;
23 }
24
25 res.type('txt').send('404 - Not found');
26}
27
28app
29 .use(express.static(path.join(__dirname, 'public')))
30 .use(error404)
31 .listen(PORT, () => console.log(`Listening on ${ PORT }`));
32