This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
forked from yschnell/baby-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (47 loc) · 1.6 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const upload = require('./file-upload.js');
const app = express();
const publicFolder = path.join(__dirname, 'public');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(publicFolder));
app.set('views', './views');
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.send('hello world')
});
app.get('/home', (req, res) => {
res.sendFile('homepage.html', { root: publicFolder })
});
app.put('/home', (req, res) => {
res.json({'good' : 'yep'})
});
app.get('/test-ejs', (req, res) => {
res.render('test', { title: 'Hey'})
});
app.get('/test-ejs2', (req, res) => {
res.render('test2', {title: 'Hey Page', users: ['Bob', 'John', 'Jane']})
});
app.get('/test-ejs3', (req, res) => {
res.render('test3', {title: 'Form Page'})
});
app.post('/test-ejs3', (req, res) => {
console.log(JSON.stringify(req.body));
res.json(req.body)
});
app.get('/number/:id', (req, res) => {
res.send(`The number is ${req.params.id}`)
});
app.get('/upload-profile-picture', (req, res) => {
res.sendFile('upload_profile_picture.html', { root: publicFolder })
});
app.post('/upload-profile-picture', upload.single('profile_pic'), (req, res) => {
const { file, fileValidationError } = req
if (!file) {
return res.status(400).send('Please upload a file');
}
console.log(file)
res.send(`<div>You have uploaded this image: <br/> <img src="http://localhost:3000/uploads/${req.file.filename}" width="500" /></div>`);
})
app.listen(3000,() => console.log('Server running on port 3000'));