node.js - How to start expressjs(nodejs) as production mode -


i want start express server in production mode. have tried running following command

sudo node_env=production node server.js 

while

sudo node server.js 

starts server in development mode out problem. first command starts server in production mode homepage opens blank black bar on top. have 2 request fail

request url:http://localhost:3000/public/build/js/dist.min.js request method:get status code:404 not found\  request url:http://localhost:3000/public/build/css/dist.min.css request method:get status code:404 not found 

i indeed have requests getting success in development mode. couldnt figure out source these requests, while there no such files or request in development mode. working on boiler plate code of mean.io .

//server/config/config.js 'use strict';  // utilize lo-dash utility library var _ = require('lodash'),     fs = require('fs');  // load configurations // set node environment variable if not set before process.env.node_env = ~fs.readdirsync('./server/config/env').map(function(file) {     return file.slice(0, -3); }).indexof(process.env.node_env) ? process.env.node_env : 'development';  // extend base configuration in all.js environment // specific configuration module.exports = _.extend(     require('./env/all'),     require('./env/' + process.env.node_env) || {} ); 

following express.js

//server/config/express.js 'use strict';  /**  * module dependencies.  */ var express = require('express'),     favicon = require('static-favicon'),     morgan = require('morgan'),     compression = require('compression'),     bodyparser = require('body-parser'),     methodoverride = require('method-override'),     cookieparser = require('cookie-parser'),     session = require('express-session'),     errorhandler = require('errorhandler'),     mean = require('meanio'),     consolidate = require('consolidate'),     mongostore = require('mean-connect-mongo')(session),     flash = require('connect-flash'),     helpers = require('view-helpers'),     config = require('./config'),     expressvalidator = require('express-validator'),     apppath = process.cwd(),     util = require('./util'),     assetmanager = require('assetmanager'),     fs = require('fs'),     grid = require('gridfs-stream');  module.exports = function(app, passport, db) {      var gfs = new grid(db.connections[0].db, db.mongo);      app.set('showstackerror', true);      // prettify html     app.locals.pretty = true;      // cache=memory or swig dies in node_env=production     app.locals.cache = 'memory';      // should placed before express.static     // ensure assets , data compressed (utilize bandwidth)     app.use(compression({         // levels specified in range of 0 9, where-as 0         // no compression , 9 best compression, slowest         level: 9     }));      // use logger development environment     if (process.env.node_env === 'development') {         app.use(morgan('dev'));     }      // assign template engine .html files     app.engine('html', consolidate[config.templateengine]);      // set .html default extension     app.set('view engine', 'html');      // set views path, template engine , default layout     app.set('views', config.root + '/server/views');      // enable jsonp     app.enable('jsonp callback');      // cookieparser should above session     app.use(cookieparser());      // request body parsing middleware should above methodoverride     app.use(expressvalidator());     app.use(bodyparser());     app.use(methodoverride());     app.use(cookieparser());      // import asset file     var assets = require('./assets.json');     assetmanager.init({         js: assets.js,         css: assets.css,         debug: (process.env.node_env !== 'production'),         webroot: 'public/public'     });      // add assets local variables     app.use(function(req, res, next) {         res.locals.assets = assetmanager.assets;         next();     });      // express/mongo session storage     app.use(session({         secret: config.sessionsecret,         store: new mongostore({             db: db.connection.db,             collection: config.sessioncollection         })     }));      // dynamic helpers     app.use(helpers(config.app.name));      // use passport session     app.use(passport.initialize());     app.use(passport.session());      //mean middleware modules before routes     app.use(mean.chainware.before);      // connect flash flash messages     app.use(flash());      // setting fav icon , static folder     app.use(favicon());      app.get('/modules/aggregated.js', function(req, res) {         res.setheader('content-type', 'text/javascript');         res.send(mean.aggregated.js);     });      function themehandler(req, res) {          res.setheader('content-type', 'text/css');          gfs.files.findone({             filename: 'theme.css'         }, function(err, file) {              if (!file) {                 fs.createreadstream(process.cwd() + '/public/system/lib/bootstrap/dist/css/bootstrap.css').pipe(res);             } else {                 // streaming gridfs                 var readstream = gfs.createreadstream({                     filename: 'theme.css'                 });                  //error handling, e.g. file not exist                 readstream.on('error', function(err) {                     console.log('an error occurred!', err.message);                     throw err;                 });                  readstream.pipe(res);             }         });     }      // override file allow swap themes     // keep same public path can make use of bootstrap assets     app.get('/public/system/lib/bootstrap/dist/css/bootstrap.css', themehandler);      app.get('/modules/aggregated.css', function(req, res) {         res.setheader('content-type', 'text/css');         res.send(mean.aggregated.css);     });      app.use('/public', express.static(config.root + '/public'));      mean.events.on('modulesfound', function() {          (var name in mean.modules) {             app.use('/' + name, express.static(config.root + '/' + mean.modules[name].source + '/' + name + '/public'));         }          function bootstraproutes() {             // skip app/routes/middlewares directory meant             // used , shared routes further middlewares , not             // route             util.walk(apppath + '/server/routes', 'middlewares', function(path) {                 require(path)(app, passport);             });         }          bootstraproutes();          //mean middlware modules after routes         app.use(mean.chainware.after);          // assume "not found" in error msgs 404.         // silly, valid, can whatever like, set properties,         // use instanceof etc.         app.use(function(err, req, res, next) {             // treat 404             if (~err.message.indexof('not found')) return next();              // log             console.error(err.stack);              // error page             res.status(500).render('500', {                 error: err.stack             });         });          // assume 404 since no middleware responded         app.use(function(req, res) {             res.status(404).render('404', {                 url: req.originalurl,                 error: 'not found'             });         });          // error handler - has last         if (process.env.node_env === 'development') {             app.use(errorhandler());         }     }); }; 

i did not make changes in either of above files after starting code mean.io. production mode

this solved running following commands before starting server

grunt cssmin grunt uglify 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -