我刚开始在Express框架中使用node.js,我正在尝试了解内置路由的工作原理。我发现了一个" main"路由器可以定义其他"子路由"使用。现在,我的应用程序最初发出一个get请求,从MySQL数据库加载一个下拉列表。我添加了一个演示按钮,该按钮应该取下拉列表中的值并使用它作为我的子路径的查询参数发出请求。当单击该子路径的按钮时,我得到404.
我的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 app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// 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(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
// 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;
我的index.js(主要路线):
var express = require('express');
var router = express.Router();
var models = require('../models');
router.use('/savings', require('./savings.js'));
/* GET home page with locations and their initial data collection dates */
router.get('/', function(req, res, next) {
models.Location.findAll({
attributes: ['locationName', 'initializationDate']
}).then(function(locations) {
res.render('index', {
title: 'Solar Data Savings',
locations: locations
});
});
});
module.exports = router;
savings.js(子路线):
var express = require('express');
var router = express.Router();
var models = require('../models');
/* GET calculate solar data savings and reroute */
router.get('/savings', function(req, res, next) {
req.param('locationID');
models.Bank.findAll({
attributes: ['bankID'],
include: [{
model: Location,
where: { locationID: Sequelize.col('bank.locationID') }
}]
}).then(function(banks) {
res.render('index', {
title: 'Solar Data Savings',
banks: banks
});
});
});
module.exports = router;
index.pug:
extends layout
block content
div(class="container-fluid")
h1= title
p This is the #{title} project website
form(action="/savings")
div(class="form-group")
label(for="locations")
div(class="col-sm-4")
select(id="locations" class="form-control")
-for(var i = 0; i < locations.length; i++) {
option(value="#{locations[i].dataValues.locationID") #{locations[i].getLocationName()}
-}
div(class="col-sm-4")
input(type="submit", value="Get Bank")
我相信我误解了路由的细微差别,而且我已经在网上搜索了这个特殊问题的解决方案而没有运气。非常感谢
答案 0 :(得分:0)
您的服务器上的储蓄路线设置为/savings/savings
,而您的表单正在调用/savings
。更改表单或更改服务器端:
在 savings.js 中,更改
router.get('/savings', function(req....
到
router.get('/', function(req....
此外,您正在使用get
提交表单。也许你需要将其改为
router.post('/', function(req...
答案 1 :(得分:0)
只需在 index.pug 中进行以下更改:
extends layout
block content
div(class="container-fluid")
h1= title
p This is the #{title} project website
form(action="/savings/savings")
div(class="form-group")
label(for="locations")
div(class="col-sm-4")
select(id="locations" class="form-control")
-for(var i = 0; i < locations.length; i++) {
option(value="#{locations[i].dataValues.locationID") #{locations[i].getLocationName()}
-}
div(class="col-sm-4")
input(type="submit", value="Get Bank")
实际上你的路由错误了:
目前您正在呼叫: your_url:port / savings
但应该是: your_url:port / savings / savings
线路需要更正
FROM:form(action =“/ savings”)
TO:form(action =“/ savings / savings”)