How can i most simply host static content with Nodejs?
- User
How can i most simply host static content with Nodejs?
Assistant
Here are a few simple ways to host static content with Node.js:
- Use the built-in
httpmodule:
const http = require('http'); const fs = require('fs'); http.createServer((req, res) => { fs.readFile('./index.html', (err, data) => { if (err) throw err; res.writeHead(200, {'Content-Type': 'text/html'}); res.end(data); }); }).listen(8000);- Use the
serve-staticmiddleware:
const express = require('express'); const serveStatic = require('serve-static'); const app = express(); app.use(serveStatic(__dirname + '/public')); app.listen(3000);- Use the
staticmiddleware in Express:
const express = require('express'); const app = express(); app.use(express.static('public')); app.listen(3000);- Use the built-in
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.