forked from koldovsky/698-team-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (32 loc) · 1019 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
34
35
36
37
// This is simple sample how to serve static website and save form data to file with NodeJS
// Author: programming mentor
// Usage:
// 0. Initialize package.json
// npm init -y
// 1. Install dependencies:
// npm i express body-parser
// 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('/contact', (req, res) => {
const fs = require('fs');
fs.appendFile('./contacts.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');