Skip to content

Instantly share code, notes, and snippets.

@nekomimi-daimao
Created August 18, 2020 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekomimi-daimao/e86023cb7305d7cd42a8e2aa34132c26 to your computer and use it in GitHub Desktop.
Save nekomimi-daimao/e86023cb7305d7cd42a8e2aa34132c26 to your computer and use it in GitHub Desktop.
show json-server start page with --static
const fs = require('fs');
const moduleId = "json-server";
const serveUrl = ["/", "/script.js", "/style.css", "/favicon.ico",];
module.exports = (request, response, next) => {
if (serveUrl.includes(request.url)) {
const fileName = request.url == "/" ? "/index.html" : request.url;
const path = require.resolve(moduleId).split(moduleId)[0] + moduleId + "/public" + fileName;
fs.readFile(path, (err, data) => {
if (!err) {
response.end(data);
}
});
return;
}
next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment