2018-10-16 14:24:56 +10:30

47 lines
1.2 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("/home/tom/Repos/WebDev-Assignment-2/404.html");
});
// 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(2019, function (){
console.log("Listening on port 2019");
});
// 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({});
}