nodejs单独的代码包括中间件use()

时间:2015-07-22 12:23:40

标签: javascript node.js express routes

我是nodejs的新手,下面是我app.js express --sessions --css less --hogan app我安装的​​app.js命令。
我尝试将route_handler.js中的注释行移至新文件调用require('./route_handler.js');

我不确定我是否正确我只在app.js中添加了route_handler.js,我是否必须在express()中导出,但它是require
怎么解决?

var express = require('express'); var path = require('path'); // var favicon = require('serve-favicon'); // var logger = require('morgan'); // var cookieParser = require('cookie-parser'); // var bodyParser = require('body-parser'); // var routes = require('./routes/index'); // var users = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'hjs'); // end: view engine setup require('./route_handler.js'); // // uncomment after placing your favicon in /public // //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); // app.use(logger('dev')); // app.use(bodyParser.json()); // app.use(bodyParser.urlencoded({ extended: false })); // app.use(cookieParser()); // app.use(require('less-middleware')(path.join(__dirname, 'public'))); // app.use(express.static(path.join(__dirname, 'public'))); // app.use('/', routes); // app.use('/users', users); // // catch 404 and forward to error handler // app.use(function(req, res, next) { // var err = new Error('Not Found'); // err.status = 404; // next(err); // }); // // error handlers // 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 }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app; 是否已经执行了文件中的代码?

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// var app = express();


var routes = require('./routes/index');
var users = require('./routes/users');



// uncomment after placing your favicon in /public
//express().use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
express().use(logger('dev'));
express().use(bodyParser.json());
express().use(bodyParser.urlencoded({ extended: false }));
express().use(cookieParser());
express().use(require('less-middleware')(path.join(__dirname, 'public')));
express().use(express.static(path.join(__dirname, 'public')));

express().use('/', routes);
express().use('/users', users);

// catch 404 and forward to error handler
express().use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

route_handler.js

$service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
$provider = NULL;
$lines = array();
while ($data = $service->fetch_object()) {
    $close = false;
    if ($something === $another) {
        $close = true;
    }

    if ( $provider != $data->provider ) {  
        $lines[] = '<div class="provider">'.$data->provider.'</div>';
        $provider = $data->provider;
    }

    if ($close === true ) { 
        $lines[] = '<div class="specific">add container for just a specific object when close === true within the while loop</div>';  
    } else {
        $lines[] = '<div>show standard container on specific object</div>';
    }   
}

foreach($lines AS $line) {
    echo $line;
}

// expore express()??

1 个答案:

答案 0 :(得分:3)

您的问题是,route_handler.js脚本会在每次express次调用中创建新的use个实例,而其中没有一个实例会连接到您在app.js中创建的应用。< / p>

你应该做的是:

route_handler.js

// !!!NOTE: no express = require !!!
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');


var routes = require('./routes/index');
var users = require('./routes/users');



function setupRoutes(app)
{
  // uncomment after placing your favicon in /public
  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  ...
}

module.exports = setupRoutes;

和app.js

...
require('./route_handler.js')(app);
...