Here a simple approch to serve static content using a node.js server.
It allows any origins
The intent is for DEV purposes, is not a prod ready snippet
const host = "0.0.0.0";
const port = 4200;
const express = require("express");
const app = express();
const website = "public";
app.all('*', function(req, res, next){
res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.set('Access-Control-Expose-Headers', 'Content-Length');
res.set('Access-Control-Allow-Credentials', 'true');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type'); // add the list of headers your site allows.
if ('OPTIONS' == req.method) return res.send(200);
next();
});
app.use(express.static(website)); //use static files in ROOT/public folder
app.get("/", function(request, response){ //root dir
response.status(200).send();
});
app.listen(port, host);
0 Comments