47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
var express = require('express'); // Express
|
|
var bodyParser = require('body-parser'); // Data conversion, JSON parsing
|
|
|
|
// Set up Express
|
|
var app = express();
|
|
|
|
app.use(express.static("./")); // Serve static files from root
|
|
app.use(express.json()); // Parse JSON in requests
|
|
app.use("/", (req, res, next) => // Finally send a 404 page
|
|
{
|
|
res.status(404).sendFile("404.html", {"root": "./"});
|
|
});
|
|
|
|
// Root route, respond with with index.html
|
|
app.get("/", function(req, res) {
|
|
res.sendFile("./index.html");
|
|
});
|
|
|
|
// News route, return defaultNews.json
|
|
app.get("/getnews", function(req, res) {
|
|
handleGetPosts(req, res);
|
|
});
|
|
|
|
// News route, digest received post JSON
|
|
app.post("/writenews", function(req, res) {
|
|
handleIncomingPost(req, res);
|
|
});
|
|
|
|
// Start listening
|
|
app.listen(8080, function (){
|
|
console.log("Listening on port 8080");
|
|
});
|
|
|
|
// Process incoming posts
|
|
function handleIncomingPost(req, res){
|
|
var newData = req.body;
|
|
console.log("Request object:");
|
|
console.log(newData);
|
|
res.status(204).send();
|
|
}
|
|
|
|
// Read and return current posts
|
|
function handleGetPosts(req, res)
|
|
{
|
|
res.json({});
|
|
}
|