-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
77 lines (65 loc) · 2 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Dependencies
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const cors = require('cors');
const fs = require('fs');
// General setup
const buffer = fs.readFileSync('./data.json');
const restaurants = JSON.parse(buffer);
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use('/static', express.static('uploads'));
// File upload setup
var storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, './uploads');
},
filename: function(req, file, callback) {
callback(null, file.originalname);
}
});
var upload = multer({ storage: storage }).single('image');
// Routes
app.get('/restaurants', (req, res) => {
if (restaurants.length > 0) {
timeoutDuration = Math.floor(Math.random() * 5000);
setTimeout(() => res.json(restaurants), timeoutDuration);
} else {
console.log(404, 'all');
res.status(404).json({ error: 'Restaurant not found :_(' });
}
});
app.get('/restaurants/:id', (req, res) => {
const restaurant = restaurants.find(item => item.id === req.params.id);
if (restaurant) {
console.log(restaurant.name);
timeoutDuration = Math.floor(Math.random() * 5000);
setTimeout(() => res.json(restaurant), timeoutDuration);
} else {
console.log(404, req.params.id);
res.status(404).json({ error: 'Restaurant not found :_(' });
}
});
// Save new restaurant
app.post('/restaurants', function(req, res) {
const nextIndexObj = { id: restaurants.length };
const newRestaurant = Object.assign(nextIndexObj, req.body);
restaurants.push(newRestaurant);
console.log(newRestaurant);
res.json(newRestaurant);
});
// Upload a photo
app.post('/upload', function(req, res) {
console.log('upload called');
upload(req, res, function(err) {
if (err) {
console.log(err);
return res.end('Error uploading file.');
}
res.end('File is uploaded successfully!');
});
});
console.log(`Starting server on port 8080`);
app.listen(8080);