Skip to content

Instantly share code, notes, and snippets.

@Fishezzz
Last active June 11, 2022 10:03
Show Gist options
  • Save Fishezzz/942bd3a5ba2dc8a80bab6f71ef9b257c to your computer and use it in GitHub Desktop.
Save Fishezzz/942bd3a5ba2dc8a80bab6f71ef9b257c to your computer and use it in GitHub Desktop.
Quick & dirty node webserver to server files and directories
// npm init
// npm install @hapi/hapi @hapi/inert
'use strict'
const Hapi = require('@hapi/hapi');
const init = async () => {
const server = Hapi.server({
port: 3210,
host: 'localhost',
routes: {
cors: true
}
});
await server.register(require('@hapi/inert'));
if (process.argv.length > 2 && process.argv[2] != undefined && process.argv[2] != "") {
var dir = process.argv[2];
} else {
var dir = ".";
}
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: dir,
index: ['index.html', 'main.html'],
listing: true
}
}
});
await server.start();
console.log('Server running at: %s', server.info.uri);
console.log('Serving directory: "%s"', dir);
};
init();
#!/bin/bash
( node ~/webserver/server.js "$1" ) || { echo "Trying with modules"; node ~/webserver/with-modules.js "$1"; }
// npm init
// npm install @hapi/hapi @hapi/inert
'use strict'
import Hapi from '@hapi/hapi';
import inert from '@hapi/inert';
const init = async () => {
const server = Hapi.server({
port: 3210,
host: 'localhost',
routes: {
cors: true
}
});
await server.register(inert);
if (process.argv.length > 2 && process.argv[2] != undefined && process.argv[2] != "") {
var dir = process.argv[2];
} else {
var dir = ".";
}
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: dir,
index: ['index.html', 'main.html'],
listing: true
}
}
});
await server.start();
console.log('Server running at: %s', server.info.uri);
console.log('Serving directory: "%s"', dir);
};
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment