-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (42 loc) · 1.61 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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const fs = require('fs');
const multer = require('multer')
const functions = require('./functions')
const app = express();
const port = 3000;
// Initialization of app variable
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, 'angular-src/dist/angular-src')));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Routes
// Managing the image upload using multer
app.post("/upload-photo", multer({dest: "./public/photos"}).single('image'), function(req, res) {
functions.storePhoto(req, res)
});
app.get('/api/photos', (req,res) => {
res.send(fs.readFileSync('./photo-register.json'));
});
// We add those routes so that the static photos and ressources can be displayed on the front-end
app.get('/public/photos/:myPhoto',(req, res) => {
var myPhoto = req.params.myPhoto;
res.sendFile(path.resolve('public/photos/' + myPhoto));
});
app.get('/public/ressources/:myRessource',(req, res) => {
var myRessource = req.params.myRessource;
res.sendFile(path.resolve('public/ressources/' + myRessource));
});
app.get('*', (req, res) => {
res.sendFile(path.resolve('./angular-src/dist/angular-src/index.html'));
});
// Listen to port 3000
app.listen(port, () => {
console.log(`Demarrage du serveur sur le port ${port}`);
});