在NodeJS中设置路由参数

时间:2015-08-20 16:28:51

标签: javascript node.js express

frontend.jade

doctype html
html
  head
    meta(charset='utf-8')
    //if lt IE 9
      script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js')
    // [if gte IE 9] <!
    script(type='text/javascript', src='http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js')
    // <![endif]
    script(type='text/javascript', src='http://ajax.aspnetcdn.com/ajax/globalize/0.1.1/globalize.min.js')
    script(type='text/javascript', 
    script(type='text/javascript', src='javascripts/file1.js')
    script(type='text/javascript', src='javascripts/file2.js')
  body
    #db(style='position:relative;')  
    #db2(style='position:relative;')  
    #pGC(style='height:800px; width:1000px; margin: 0 auto')

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 routes = require('./routes/index');
var users = require('./routes/users');
var info = require('./routes/info');

var app = express();


app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');


app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));



app.use('/', routes);
app.use('/users', users);
app.use('/info', info);

// 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;

info.js

var express = require('express');
var request = require('request');
var fs = require('fs');

var router = express.Router();

router.get('/', function (req, res) {

   res.render('frontend');

    });
   module.exports= router;

当我在info.js中添加一个像

这样的参数时
 router.get('/:name',function (req,res) {
     res.render('frontend');
   }

现在而不是在/javascripts/file1.js和/javascripts/file2.js中查找静态文件

它是GET trans / javascriptsfile1.js,它返回404.

文件位于public / javascripts / file1.js

2 个答案:

答案 0 :(得分:0)

frontend.jade 中,

更改

script(type='text/javascript', src='javascripts/file1.js')
script(type='text/javascript', src='javascripts/file2.js')

script(type='text/javascript', src='/javascripts/file1.js')
script(type='text/javascript', src='/javascripts/file2.js')

问题是javascript src是相对于渲染的url(http://localhost/info)。所以,先用&#39; /&#39;将从根网址(http://localhost/)开始,表示.static将提供公共文件夹中的文件。

答案 1 :(得分:0)

当您致电app.use时,您的服务器会将该功能链接到每个请求。如果您在以前的所有函数中调用next(),它最终会落入此错误函数。

当您添加参数时,请求不会被路由引起并继续通过中间件链(最终到达错误函数)。