Last active
June 11, 2022 10:03
-
-
Save Fishezzz/942bd3a5ba2dc8a80bab6f71ef9b257c to your computer and use it in GitHub Desktop.
Quick & dirty node webserver to server files and directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
( node ~/webserver/server.js "$1" ) || { echo "Trying with modules"; node ~/webserver/with-modules.js "$1"; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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