-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
52 lines (38 loc) · 1.37 KB
/
server.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
const util = require('util');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const port = 3001;
//Configuring the body-parser middleware to handle POST requests
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
app.get('/', function(request, response) {
console.log('Hello from Express!');
response.sendFile(path.join(__dirname + '/index.html'));
});
app.post( '/preferences', function( request, response){
console.log( 'in post preferences');
let preferencesJSON = request.body;
fs.writeFileSync('data/' + preferencesJSON.filename, JSON.stringify(preferencesJSON));
console.log( preferencesJSON);
response.end("{saved: true}");
});
app.get( '/preferences/:filename', function( request, response){
response.end(fs.readFileSync('data/' + request.params.filename));
});
app.use( '/css', express.static('css/'));
app.use( '/js', express.static('js/'));
app.use(function(request, response){
if( request.status === 404) {
console.log("Returning 404 error page");
response.sendFile(path.join(__dirname + '/404.html'));
}
});
app.listen(port, function(err) {
if(err) {
console.log("The following error has occured : \n" + err);
}
console.log('server is listening on ' + port);
});