我在使用 Express 的 node.js 中有一个网站的前端和一个后端。
现在我想添加另一个使用 Java 的前端。我想要一个可以填写表格并添加用户的应用程序。在 btn_create_user_pressed 上,我想在 node.js 中调用我的服务器并使用以下反馈创建它:用户已创建-用户已存在等。
我以为我可以使用 POST 调用 localhost:3000/adduser 但在我的路由中,如果创建成功,它会重定向
如何修改我的路线和/或我的视图,以便重定向将不依赖于我的 addUser 操作,因此我将能够从任何其他前端调用它。
这是我的 newuser.jade
extends layout
block content
h1= title
form#formAddUser(name="adduser",mehod="post",action="/adduser")
input#inputUserName(type="text", placeholder="Votre Nom", name="username")
input#inputUserEmail(type="text", placeholder="Votre Email", name="useremail")
button#btnSubmit(type="submit") Envoyer
它调用我的路由 adduser:
/* POST to Add User Service */
router.post('/adduser', function(req, res) {
// Set our internal DB variable
var db = req.db;
// Get our form values. These rely on the "name" attributes
var userName = req.body.username;
var userEmail = req.body.useremail;
// Set our collection
var collection = db.get('usercollection');
// Submit to the DB
collection.insert({
"username" : userName,
"email" : userEmail
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// And forward to success page
res.redirect("userlist");
}
});
});