我不明白我的路由有什麼問題。爲什麼沒有找到資源,如果我用app.js文件定義url模式回調?node.js中的簡單路由
app.get('/myroute', function(req, res) {
res.send('Hello /myroute from app.js')
});
var express = require('express');
/*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', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
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);
});
app.get('/myroute', function(req, res) {
res.send('Hello /myroute from app.js')
});
如果我取消下一行:
var routes = require('./routes/index');
var users = require('./routes/users');
app.use('/', routes);
app.use('/users', users);
而且把我的URL模式回調./routes/index它會工作!
router.get('/myroute', function(req, res) {
res.send('Hello/from /route/index.js');
});
是的,我用它。就像我想的那樣,我不會包含可能不相關的代碼。我可能找到了答案。 – Ark 2014-10-29 09:39:18