我正在使用带有jade的expressjs。我正在尝试用输入做一个阶乘,但我无法显示答案,例如,如果我输入5结果是120,但显示一个很大的错误。我该怎么做? 你能帮帮我吗? error
routes/index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index.jade', { title: 'Express' });
});
router.get('/factorial', function(req, res, next) {
res.render('factorial.jade', { title: 'Factorial' });
});
module.exports = router;
views/factorial
extends layout
block content
h1= title
p Welcome to #{title}
p Ingrese un numero
form(method="POST", action="/factorial", name="myForm",onsubmit='return validateForm()')
input(type='number', name='numero')
input(type='submit', value='Enviar')
<strong>archive app.js</strong>
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
//agregado
var bodyParser = require('body-parser');
var engine = require('ejs-mate');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// instruct the app to use the `bodyParser()` middleware for all routes
app.use(bodyParser());
// view engine setup
app.engine('ejs', engine);
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: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'bower_components')));
app.use('/', routes);
app.use('/users', users);
app.post('/factorial', function(req, res) {
var num=req.body.numero;
dd(num);
for(var i=1;i<=num;i++)
i=i*num;
res.send('/factorial'+i);
});
// 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;
答案 0 :(得分:0)
我至少可以看到一个错误:
res.send(/factorial + i)
没有任何已知的发送答案的方式,可能是导致错误的原因。事实上,errro告诉你发送了一个无效的结果代码(45),可能因为你的响应的第一个字符(/)被解释为返回代码,因为你没有提供正确的响应标题
此外,除了ddd(num)调用是一种神奇的东西,你计算一个阶乘是不正确的。你发送5,(结果应该 5 * 4 * 3 * 2 * 1 = 120 )。然后
循环传递它返回1
循环传递它返回10
并退出,因为我目前大于5
数字工作版可以是:
app.post('/factorial', function(req, res) {
var num=req.body.numero;
dd(num); // << dont know what it does
function fact(n) {
if (n < 2) return n
else return n*fact(n-1)
}
var result = fact(num)
...
});
要正确发送结果,您应该阅读相关文档。太久没在这里回答了
答案 1 :(得分:0)
试试这个:
var express = require("express")
var app = express()
function factorial(num) {
if (num < 0)
return "INVALID"
if (num == 1 || num == 0)
return 1
else return num * factorial(num - 1)
}
app.get("/factorial/:num", function (req, res) {
var num = req.params.num;
res.status(200).send(""+factorial(num))
})
app.listen(3000)
例如,将浏览器指向http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)。