0

I saw that you can use connect to use serve static files in a Node.js HTTP server like this:

var http = require('http');
var connect = require('connect');
var app = connect().use(connect.static(__dirname + path));
http.createServer(app).listen(8080);

How would I implement this in my current handler?

var http = require("http");
var handler = function(request, response){
    // code
}
http.createServer(handler);

Is this even possible? If so, how can I accomplish it?

baranskistad
  • 1,886
  • 1
  • 16
  • 39

1 Answers1

0

Unless you want to use an older version of connect that might not function properly, you'd have to install serve-static to do what you're trying to do. See this answer https://stackoverflow.com/a/24347442/5382465

var finalhandler = require('finalhandler')
var http = require('http')
var serveStatic = require('serve-static')

// Serve up public folder 
var serve = serveStatic('public', {'index': ['index.html', 'index.htm']})

// Create server 
var handler = http.createServer(function onRequest (req, res) {
  serve(req, res, finalhandler(req, res))
})

// Listen 
handler.listen(3000)
Community
  • 1
  • 1
govgo
  • 555
  • 4
  • 17