Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

node.js - static content via apache? not found via node

node.js - static content via apache? not found via node


I'm new to node.js just playing around with socket and node.

I've this server:

var fs = require('fs')
    , http = require('http')
    , socketio = require('');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(1234, function() {
    console.log('Listening at: http://localhost:1234');

socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);

I see it doesn't load normal html static content, like css and js files from server.js folder.

How do I make node.js load these?

Or how do I rewrite my server, so that index.html is served by Apache and all the message pinging is done with node?

Problem courtesy of: user796443


If you are making a simple site, with not much static content, you can send it all with node + express. If your site is heavy on images/video/etc, then having apache to deal with those things is a good solution. Just remember that node and apache can't both listen on port 80. Set up apache to listen on port 80, then use mod_proxy (the [P] flag in htaccess) to redirect requests you want going to node over a different port.

Solution courtesy of: Chris


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

node.js - static content via apache? not found via node


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription