I've got a really simple server I'm trying to setup which uses Mongoose (to connect to MongoDB) and Express (to serve up files). The app.js file looks like the following:
var express = require('express');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log("MongoDB connection is open.");
});
// Mongoose Schema definition
var Schema = mongoose.Schema;
var LocationSchema = new Schema({
//insert schema definition here
});
// Mongoose Model definition
var LocationsCollection = mongoose.model('locations', LocationSchema);
// Bootstrap express
var app = express();
// URL management
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// Start the server
app.listen(3000);
So, I make sure I start the mongod.exe file (and it's running in the background). Then, I open another command prompt, cd to my working directory and launch my server code using -node app.js. The code starts and indeed I see the message saying that the "MongoDB connection is open". So, the mongoose connection seems to work fine. But, then when I open a browser and type in the URL: http://127.0.0.1:3000 I immediately get an error saying:
Error: ENOENT, stat 'D:\Users\myname\Desktop\Server\index.html'
Now, I know for a fact that the index.html file is indeed in that folder (which is the same folder as my app.js)... so I don't know why I'm getting this error. I've seen other threads saying I need to create an npm folder in my roaming directory... but it's already there. It seems to always be 'read-only' even if I uncheck that box and apply the settings... but, I don't know what could be wrong. I'm using the latest versions of node and mongodb. The issue clearly seems to be occurring when the express server tries to serve up the index.html file... but I can't figure out where the error is occurring. Any thoughts?