-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathserver.js
58 lines (48 loc) · 1.19 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
const { conn, User, Thing } = require('./db');
const express = require('express');
const app = express();
const path = require('path');
app.use(express.json());
app.use('/dist', express.static('dist'));
app.get('/', (req, res)=> res.sendFile(path.join(__dirname, 'index.html')));
app.post('/api/things', async(req, res, next)=> {
try {
res.status(201).send(await Thing.create(req.body));
}
catch(ex){
next(ex);
}
});
app.get('/api/things', async(req, res, next)=> {
try {
res.send(await Thing.findAll());
}
catch(ex){
next(ex);
}
});
app.get('/api/users', async(req, res, next)=> {
try {
res.send(await User.findAll());
}
catch(ex){
next(ex);
}
});
const port = process.env.PORT || 3000;
app.listen(port, ()=> console.log(`listening on port ${port}`));
const init = async()=> {
try {
await conn.sync({ force: true });
const [moe, larry, lucy, ethyl] = await Promise.all(
['moe', 'larry', 'lucy', 'ethyl'].map( name => User.create({ name }))
);
const [foo, bar, bazz, quq, fizz] = await Promise.all(
['foo', 'bar', 'bazz', 'quq', 'fizz'].map( name => Thing.create({ name }))
);
}
catch(ex){
console.log(ex);
}
};
init();