所以我最近开始使用expressjs。现在我的get请求中的路由有问题。 这是我的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 session = require('express-session');
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')));
var login=require('./routes/login');//get a js file login.js
app.use('/user_form',login);
在loginjs中我有两个请求:
var express = require('express');
var router = express.Router();
var bodyparser = require("body-parser");
var urlencodedParser = bodyparser.urlencoded({ extended: false });
router.post("/", function (req,res,next) {
var sess = req.session;
var answer = {
username: req.body.username,
password: req.body.password
};
if (users[answer.username] && users[answer.username].password ==
answer.password) {
users[answer.username].logged = true;
sess.logged = answer.username;
res.redirect("/");
}
else {
res.render("fail");
}
});
router.get("/logged", function (req, res) {
sess = req.session;
res.send(sess.logged);
});
这个帖子有效,但得到的却没有,它总是给我500错误,我无法弄清楚问题。
这是客户端的代码:
$.ajax({
type: "GET",
url: "user_form/logged",
dataType: "xml"
}).done(function (res) {
alert(res);
}).fail(function (jqXHR, textStatus, errorThrown) {
alert("AJAX call failed: " + textStatus + ", " + errorThrown);
});
任何帮助表示赞赏!