javascript - Express server not serving static files -
i'm using express , socket.io write app, server can't find static files are. realize question has been asked multiple times, none of proposed solutions worked me. i've tried different ways reference public folder express.static()
or rearrange code structure, still no luck.
code structure:
/node_modules /public /css index.css /html index.html /js /src /server /models index.js package.json
index.js:
// modules needed var express = require('express'), http = require('http'), bodyparser = require('body-parser'), logger = require('logger'), mongoose = require('mongoose'), io = require('socket.io'), path = require('path'), methodoverride = require('method-override'), user = require('./src/server/models/user'); // connect mongodb mongoose.connect('mongodb://localhost:27017/' + name); var db = mongoose.connection;*/ var uristring = process.env.mongolab_uri || process.env.mongohq_url || 'mongodb://localhost/hellomongoose'; mongoose.connect(uristring, function (err, res) { if (err) { console.log ('error connecting to: ' + uristring + '. ' + err); } else { console.log ('succeeded connected to: ' + uristring); } }); // set var app = express(); var server = http.server(app); var ioserver = io(server); app.use(bodyparser.json({})); app.use(bodyparser.urlencoded({ extended: true })); app.use(methodoverride()); app.use(bodyparser()); app.use(express.static(__dirname + './public')); // connect socket ioserver.on('connection', function(socket){ // })
you'll need serve directory contains index.html
file:
app.use(express.static(__dirname + '/public/html'));
you need ensure route specified /public/html
, not ./public/html
.
while solves problem, i'll recommend place index.html
file @ root of public
directory. recommended code structure:
/node_modules /public /css index.css /js index.html /src /server /models index.js package.json
Comments
Post a Comment