forked from VidyBack/postDataOnServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (24 loc) · 845 Bytes
/
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
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = low(adapter);
server.use(middlewares);
server.use(jsonServer.bodyParser);
server.post('/your-endpoint', (req, res) => {
// Process the request body and add/update data in db.json using lowdb
// For example:
const newData = req.body;
// Add a new post
db.get('posts').push(newData).write();
console.log('Added new post:', newData);
res.status(200).json(newData);
});
server.use(router);
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`JSON Server is running on http://localhost:${port}`);
});