forked from koldovsky/online-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (28 loc) · 925 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
// This is simple sample how to serve static website and save form data to file with NodeJS
// Author: programming mentor
// Usage:
// 1. Install dependencies: npm i
// 2. Run: server node server.js
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
app.use(bodyParser.json());
app.use(express.static(path.resolve(__dirname, './')));
app.post('/order', (req, res) => {
const fs = require('fs');
fs.appendFile('./orders.txt', JSON.stringify(req.body) + '\n', function(err) {
if (err) {
res.status(500).send('Server error');
return console.log(err);
}
console.log('Data saved: ' + JSON.stringify(req.body));
res.send('Data saved');
});
});
console.log(
'Server is running on',
process.env.PORT || 3000,
process.env.IP || '0.0.0.0'
);
app.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0');