Jack
Jack

Reputation: 165

How do I serve multiple files in Node?

Here is existing code:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

But now I split CSS in to app.css and moved app.css and index.html to /public folder.

How do I serve all /public folder? I try res.sendfile('/public'); but it causes just errors!

Current code:

var app = require('express')();
var express = require('express');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var irc = require('irc');

app.use(express.static('public'));
app.listen(3000);

see pages being served now on port 3000 but no connection to the chat.

Upvotes: 2

Views: 2519

Answers (1)

Piotr Jaworski
Piotr Jaworski

Reputation: 584

As in Vohuman's comment:

var path = require('path');

app.use(express.static(path.join(__dirname, '/public')));

Upvotes: 1

Related Questions