ajax 发布到 Node.js Express 应用程序?
我试图在使用 ajax 插入数据库之前进行身份验证
$('#button').click(function () {
$.post('/db/', {
stuff: { "foo" : "bar"}
}, callback, "json");
});
这是我的 node.js 代码:
server.get('/', function(req,res){
console.log(res);
res.render('index.ejs', {
locals : {
header: '#Header#'
,footer: '#Footer#'
,title : 'Page Title'
,description: 'Page Description'
,author: 'Your Name'
,analyticssiteid: 'XXXXXXX'
}
});
});
^^ 这部分工作正常。它来自样板,我可以转到本地主机并查看首页。
下一部分应该是 mongo 插入发生的地方。这给了我一个 404。
server.on('/db/', function(req,res){
console.log(req);
console.log(res);
var db = new mongo.Db( 'dbname' , new mongo.Server( 'localhost', 20003, {}), {});
db.open(function (err) {
db.collection('testCollection', function (err, collection) {
collection.insert(res.stuff);
});
});
});
我想做的是将对象 stuff
插入 testCollection。
现在我在 /db/ 上收到 404
我确信这是非常错误的。我认为我不应该使用 server.on,server.get 也不起作用。请指教,谢谢。
I'm trying to authenticate before using ajax to insert into a database
$('#button').click(function () {
$.post('/db/', {
stuff: { "foo" : "bar"}
}, callback, "json");
});
Here's my node.js code:
server.get('/', function(req,res){
console.log(res);
res.render('index.ejs', {
locals : {
header: '#Header#'
,footer: '#Footer#'
,title : 'Page Title'
,description: 'Page Description'
,author: 'Your Name'
,analyticssiteid: 'XXXXXXX'
}
});
});
^^ this part works ok. It comes from a boilerplate, I can go to localhost and see the front page.
This next part is supposed to be where the mongo insert happens. This gives me a 404.
server.on('/db/', function(req,res){
console.log(req);
console.log(res);
var db = new mongo.Db( 'dbname' , new mongo.Server( 'localhost', 20003, {}), {});
db.open(function (err) {
db.collection('testCollection', function (err, collection) {
collection.insert(res.stuff);
});
});
});
What I'm trying to do is insert the object stuff
into testCollection.
Right now I'm getting a 404 on /db/
I'm sure this is very wrong. I don't think I'm supposed to use server.on, server.get doesn't work either. Please advise, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用
server.post
,因为您正在执行 通过 jQuery POST 请求。server.on
将为服务器添加一个事件侦听器,在本例中是针对不存在的/db/
事件,该事件永远不会被任何事件触发。请花点时间并确保阅读express.js 指南、Node.js API 文档也可能会派上用场。
You have to use
server.post
, since you're doing a POST request via jQuery.server.on
will add a event listener to the server, in this case for the non-existent/db/
event, which never gets triggered by anything.Please take your time and make sure to read the express.js Guide, the Node.js API Docs might come in handy too.
由于您期待一个帖子,因此您的 /db/ 处理程序应该在
server.post
方法中定义。您收到 404 是因为服务器没有为 POST 和 /db/ 的组合定义路由。Since you're expecting a post, your /db/ handler should be defined in a
server.post
method. You're getting a 404 because the server doesn't have a route defined for the combination of POST and /db/.您还应该对数据库连接进行身份验证,并且不应在每个请求时重新连接到数据库
you also should be authenticating the db connect and shouldn't be reconnecting to the db on each request