-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathcreate-static-server.js
executable file
·30 lines (27 loc) · 1.1 KB
/
create-static-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
#!/usr/bin/env node
const http = require('http');
const path = require('path');
const serveHandler = require('serve-handler');
/**
* Creates an HTTP server that serves static files from a directory using serve-handler.
* If a request URL starts with `/node_modules/`, it rewrites the URL and serves files from the `node_modules` directory.
*
* @param { NonNullable<Parameters<typeof import("serve-handler")>[2]> } options - Configuration options for serve-handler. Documentation can be found here: https://github.com/vercel/serve-handler
* @returns {http.Server} An instance of an HTTP server configured with the specified options.
*/
const createStaticServer = (options) => {
return http.createServer((request, response) => {
if (request.url.startsWith('/node_modules/')) {
request.url = request.url.slice(14);
return serveHandler(request, response, {
directoryListing: false,
public: path.resolve('./node_modules'),
});
}
return serveHandler(request, response, {
directoryListing: false,
...options,
});
});
};
module.exports = createStaticServer;