2.7.3 • Published 10 months ago
servatron v2.7.3
servatron
An extremely lightweight static file server that can be used as a handler or cli.
Installation
npm install --save servatron
(or) globally:
npm install -g servatron
Usage
CLI
servatron --directory dist --port 8000 --spa
Optional arguments are:
--directory (-d) pathName specify a directory to server the files from (can provider multiple)
--bind (-b) hostname what host to bind on (default: 0.0.0.0)
--port (-p) number what port to listen on (default: 8000)
--spa when a path is not found, deliver the index file
--spa-index what name spa mode should look for the html file (default: index.html)
--http2 use http2 as the server protocol
--key what key to use for http2
--cert what cert to use for http2
--ca optionally add a ca for http2
--anti-cors set all CORS headers to the most flexible
Code - HTTP 1
import http from 'http';
import servatron from 'servatron/http';
const staticHandler = servatron({
directory: './dist',
spa: true,
spaIndex: 'index.html',
resolvers: {
'**/*.ejs': (file, content) => {
response.writeHead(200, {
'content-type': 'text/html'
});
response.end(ejs.render(content.toString(), { message: 'Hello World' }));
}
}
})
// Use only the staticHandler
http.createServer(staticHandler).listen(8000)
// (or) Mix custom handler logic with staticHandler
http.createServer(function (request, response) {
if (request.url.startsWith('/api')) {
response.end('this could be an api')
return
}
staticHandler(request, response)
).listen(8000, '0.0.0.0')
Code - HTTP 2
import http2 from 'http2';
import servatron from 'servatron/http2';
const staticHandler = servatron({
directory: './dist',
spa: true,
spaIndex: 'index.html',
resolvers: {
'**/*.ejs': (file, content) => {
stream.respond({
'content-type': 'text/html',
':status': 200
});
stream.end(ejs.render(content.toString(), { message: 'Hello World' }));
}
}
})
const server = http2.createSecureServer({
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem')
});
server.on('error', (error) => console.error(error));
// Use only the staticHandler
server.on('stream', staticHandler);
server.listen(8000, '0.0.0.0');
// (or) Mix custom handler logic with staticHandler
server.on('stream', function (stream, headers) {
if (headers[':path'].startsWith('/api')) {
stream.end('this could be an api');
return;
}
staticHandler(stream, header);
});
server.listen(8000, '0.0.0.0');
2.7.0
10 months ago
2.6.1
10 months ago
2.6.0
10 months ago
2.7.2
10 months ago
2.7.1
10 months ago
2.7.3
10 months ago
2.5.0
2 years ago
2.4.3
2 years ago
2.4.1
3 years ago
2.4.0
3 years ago
2.4.2
3 years ago
2.3.2
3 years ago
2.3.1
3 years ago
2.3.0
3 years ago
2.2.3
4 years ago
2.2.2
4 years ago
2.2.1
4 years ago
2.2.0
4 years ago
2.1.8
4 years ago
2.1.7
4 years ago
2.1.6
4 years ago
2.1.4
4 years ago
2.1.5
4 years ago
2.1.3
4 years ago
2.1.2
4 years ago
2.0.3
4 years ago
2.1.1
4 years ago
2.0.4
4 years ago
2.1.0
4 years ago
2.0.1
4 years ago
2.0.0
4 years ago
1.1.3
4 years ago
1.1.2
4 years ago
1.1.1
4 years ago
1.1.0
4 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.3
5 years ago
1.0.0
5 years ago