3

I don't know what I touched but suddenly it stopped serving my static files. I have the following architecture:

  1. App

    • Routes
    • Models
  2. public

    • views
    • css
    • js
  3. server.js

The Error:

Error: Cannot find module 'html'
    at Function.Module._resolveFilename (module.js:325:15)   
...
at /Users/.../vintageAddiction/app/routes/indexRoutes.js:12:11



indexRoutes.js:

var express  = require('express');
var passport = require('passport');

var indexRouter = express.Router();
  indexRouter.route('/').get( function(req, res) {
      res.render('index.html'); // load the index.ejs file
    });


module.exports = indexRouter;

SERVER.JS:

// server.js

// set up ======================================================================
// get all the tools we need
var express  = require('express');
var app      = express();
var port     = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash    = require('connect-flash');

var morgan       = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser   = require('body-parser');
var session      = require('express-session');

var configDB = require('./app/config/configDB.js');

// configuration ===============================================================
mongoose.connect(configDB.url); // connect to our database

require('./app/config/passport')(passport); // pass passport for configuration

// routes ======================================================================
var routes       = require('./app/routes/indexRoutes.js'); // load our routes and pass in our app and fully configured passport
var adminRoutes  = require('./app/routes/adminRoutes.js');


// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser()); // get information from html forms

// app.set('view engine', 'ejs'); // set up ejs for templating

// required for passport
app.use(session({ secret: 'vintageisthelaw' })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

app.use('/', routes);
app.use('/admin', adminRoutes);

// set static files location
// used for requests that our frontend will make
app.use(express.static(__dirname + '/public/'));
app.set('views', __dirname + '/public/views');
app.set('view engine', 'jade');
// launch ======================================================================
app.listen(port);
console.log('The magic happens on :\n\n http://localhost:'+ port+'\n\n');

link to server.js

I've seen something similar to :

app.use(express.static(__dirname + '/public/views/'));

But I don't really understand why is not working Hope you can help me guys !

Despertaweb
  • 1,672
  • 21
  • 29
  • 1
    Wouldn't it be `res.render('index')`. Are you trying to serve a static html file or just a standard view? (views normally contain dynamic content). – Ash Apr 06 '16 at 15:12
  • Also, `express.static` should normally go before you use any of your routes. – Ash Apr 06 '16 at 15:18
  • I'm trying to do a CRUD, and then i'll do the front-end of the office. So it's going to be dynamic ! you're right! @AshleyB – Despertaweb Apr 06 '16 at 15:19
  • check this http://stackoverflow.com/questions/16111386/node-js-cannot-find-module-html – Deendayal Garg Apr 06 '16 at 15:27
  • If they are `views` and not `static` files, you probably don't want to put them in your public folder. they are normally located in `/views`. If you put them in the static folder, you will not 'render' the views, but simply send them to the browser (allowing no dynamic content). – Ash Apr 06 '16 at 15:36

1 Answers1

4

In express.js order of middleware declaration is very important. You must define express.static middleware earlier than any of the routes:

SERVER.JS:

.. require

// logger must be defined first if you want log all the requests
app.use(morgan('dev'));             
// after that you should define express.static middleware            
app.use(express.static(__dirname + '/public/')); 
app.set('views', __dirname + '/public/views');
app.set('view engine', 'jade');
// cookie and body parser must be defined before passport and session middleware
app.use(cookieParser());
app.use(bodyParser());

app.use(session({ secret: 'vintageisthelaw' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

// your routes must be defined in the end
app.use('/', routes);
app.use('/admin', adminRoutes);

app.listen(port);
console.log('The magic happens on :\n\n http://localhost:'+ port+'\n\n');
alexmac
  • 19,087
  • 7
  • 58
  • 69
  • view engine is a must ? I'm just using angularJS with plain Html , no jade,no ejs no other stuff @Alexander Marc – Despertaweb Apr 07 '16 at 08:10
  • 1
    Of course no, if you don't use view engine in your app you can delete this line: `app.set('view engine', 'jade');`, and uninstall `jade` package if it's installed: `npm un -S jade`. – alexmac Apr 07 '16 at 09:07
  • If I remove the : app.set('view engine', 'ejs'); it throws : Error: Cannot find module 'html' :( – Despertaweb Apr 07 '16 at 10:53
  • 3
    It's because you render html page: `res.render('index.html')`, and it's of course requires view engine. You told that you use plain html, if so, you shouldn't use `res.render`, use [res.sendFile](http://expressjs.com/en/4x/api.html#res.sendFile) instead: – alexmac Apr 07 '16 at 11:00