-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
58 lines (49 loc) · 1.64 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 express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const fs = require("file-system");
const PORT = process.env.PORT || 8080;
const app = express();
// Authentication - actual repo contains real credentials, this is the dev-test version
app.use((req, res, next) => {
const auth = { login: 'dev-test', password: 'dev_test01' };
// parse login and password from headers
const b64auth = (req.headers.authorization || '').split(' ')[1] || '';
const [login, password] = new Buffer(b64auth, 'base64').toString().split(':');
// Verify login and password are set and correct
if (login && password && login === auth.login && password === auth.password) {
// Access granted...
return next();
}
// Access denied...
res.set('WWW-Authenticate', 'Basic realm="401"'); // change this
res.status(401).send('Authentication required.'); // custom message
});
// Data parsing
app.use(
express.urlencoded({
extended: true
})
);
app.use(
bodyParser.json
({extended: true}));
app.use(
bodyParser.urlencoded ({
extended: true
})
);
app.use(express.json());
app.use(express.static("public"));
const previewRoute = require("./controllers/preview");
const storyRoute = require("./controllers/storytemplate");
const AllStories = require("./controllers/chatStories");
app.use(previewRoute);
app.use(storyRoute);
app.use(AllStories);
app.get("/", function(req, res) {
res.json(path.join(__dirname, "public/index.html"));
});
app.listen(PORT, function() {
console.log("Server listening on: http://localhost:" + PORT);
});