A
A
Alexander2016-03-04 17:34:48
Node.js
Alexander, 2016-03-04 17:34:48

What's the catch with the app.js config in NodeJS 5.7?

Good day. Due to the difference in syntax and other delights of migrating versions of Noda, I got stuck on noob things.
The current config looks like this:

<code>process.env.NODE_ENV = "development";
var express = require('express');
var app = express();
app.listen(3000);
console.log('Express started on port 3000');

var logger = require('winston');
var path = require('path');
var routes = require('./routes/index');
var users = require('./routes/users');
var nconf = require('nconf');

// view engine setup
app.set('teamplate', path.join(__dirname, 'teamplate'));
app.set('view engine', 'jade');


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

app.use(function (req, res, next) {
  if (req.url == '/chat') {
  res.render("some chat");
        res.send(env);}
  else {
      next();
  }
});

//Middleware
app.get('/', function(req, res, next){
  res.render('index', {
    body: '<b>Jrkk</b>'
    
  });
  
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});  

// production error handler (no stacktraces leaked to user)
app.use(function(err, req, res, next) {
  res.status(err.status || 500 || 404);
  res.render('error', {
    message: err.message,
    error: {
    
  }
  });
});

// development error handler (will print stacktrace)
  if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}
});</code>

The server output looks like this:
Error: Cannot find module 'jade'
   at Function.Module._resolveFilename (module.js:339:15)
   at Function.Module._load (module.js:290:25)
   at Module.require (module.js:367:17)
   at require (internal/module.js:16:19)
   at new View (/usr/lib/node_modules/express/lib/view.js:78:30)
   at EventEmitter.render (/usr/lib/node_modules/express/lib/application.js:569:12)
   at ServerResponse.render (/usr/lib/node_modules/express/lib/response.js:961:7)
   at /gdetus/routes/index.js:6:7
   at Layer.handle [as handle_request] (/usr/lib/node_modules/express/lib/router/layer.js:95:5)
   at next (/usr/lib/node_modules/express/lib/router/route.js:131:13)

Due to the fact that no one on the Toaster has the skill to speak and answer questions about the NODE_ENV environment variable, I did not understand where to put it in the files, nevertheless, at least I have Jade installed. What's the catch?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Skrolea, 2016-03-04
@Skrolea

Of course, I'm a beginner, so excuse me if I'm stupid, but what's the problem with setting NODE_ENV in package.json, for example? Like this:

"scripts": {
"prod": "NODE_ENV=production",
"dev": "NODE_ENV=development"
},

Well this is for linux, for windows set
"scripts": {
    "prod": "SET  \"NODE_ENV=production\" ,
    "dev": "SET \"NODE_ENV=development\"  
  },

ah yes. You do not have windows
PS I would still do
npm install jade --save
npm install

F
frontendthug, 2016-03-04
@frontendthug

It can't find the jade module because it needs to require that module. He tells you this right in the error. And here NODE_ENV ? According to your code, the environment does not matter here.

A
Anton Ulanov, 2016-03-04
@antonsr98

NODE_ENV nodejs is googled in 3 minutes, the first link and people explain how to register it.

D
Daniil Borovkov, 2016-03-05
@daniilborovkov

Indeed, where
?

A
Alexander N++, 2017-09-12
@sanchezzzhak

in the console, via SET PORT 3001, it is registered globally for the entire system
in nodejs, process.env.PORT is available;
In php $_ENV['port'];
updated today from 4.4 to 8.4 normal flight _)
To be honest, express annoys me: the
router looks much better and symptomatic

const
    finalhandler = require('finalhandler'),
    http = require('http'),
    Router = require('router');

let routerOptions = {},
    router = Router(routerOptions ),
    server;

router.get('/chat/test',function(req,res) {
    let ChatController = new( require('./lib/controller/chat'));
    ChatController.actionTest().then(function(result){
          res.end(result);
   });
});

server = http.createServer(function onRequest(req, res) {
                router(req, res, finalhandler(req, res));
});
 // timeout 2 min default 120000
server.listen({port: port, timeout: 60000}, function(){
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question