Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
    
Why don't you just specify the directory to serve static files in? i.e. app.use(express.static('public'))? – vincentjp 22 hours ago
    
@vincentjp Because I'm not using Express. – bjskistad 22 hours ago
1  
See Using node.js as a simple web server: stackoverflow.com/questions/6084360/… – Bruno 22 hours ago
    
But it's so much easier. – vincentjp 22 hours ago
    
@vincentjp But it's NOT what I'm using. – bjskistad 13 hours ago

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 http://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)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.