我为 $.post 调用服务器以获取 node.js 文件的 url 是什么。我一直在看 php 文件的教程,但我对调用哪些 node.js 文件感到困惑?我想将它发布到 app.js 文件还是路由文件?
这是 app.js 文件的样子:
var express = require('express')
, app = express()
, dbUserModel = require('./models/user')
, db = require('./db')
, pass = require('./config/passport')
, passport = require('passport')
, routes = require('./routes/index')
, user = require('./routes/user')
, path = require('path')
, http = require('http')
, connect = require('connect')
, mongoose = require('mongoose')
, mongoConnect = mongoose.connect('mongodb://localhost/test5');
// all environments
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser('sabkdasdkjhakhfkahf7232ujdijaw9jok&^&^@88'));
//app.use(express.cookieSession());
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
//use bottom for 404 error
app.use(function(req,res){
res.render('noPage.jade');
});
});
app.get('/', routes.index);
app.get('/register', user.mustBeLoggedOut, user.register);
app.post('/register', user.registerPost);
app.get('/login', user.mustBeLoggedOut, user.login);
app.post('/login', user.loginPost);
app.get('/userProfile', user.mustBeLoggedIn, user.userProfile);
app.get('/editUserProfile', user.mustBeLoggedIn, user.editUserProfile);
app.post('/editUserProfile', user.editUserProfilePost);
app.get('/loggedIn', user.mustBeLoggedIn, user.loggedIn);
app.get('/contactList', user.mustBeLoggedIn, user.contactList);
app.get('/search', user.search);
app.post('/search', user.searchPost);
app.get('/user/:id', user.user);
app.post('/addContact', user.addContactPost);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
console.log('Users: ' + db.collections.users);
});
这是路由文件的样子:(我知道路由文件中的代码在尝试提取与按钮关联的 user.id 时可能不正确,仍在进行中)
exports.addContactPost = function(req, res, err) {
User.findByIdAndUpdate(req.signedCookies.userid,{
friendRequest: req.body.userid
}, function(err) {
if(err) {
console.log("post2");
return res.render('addContactError', {title: 'Weblio'});
}
});
};
这是脚本文件,试图找出 URL:
$('.addContact').click(function() {
$.post('user.js',
function(data) {
$('.addContact').html(data);
}
);
if($(this).html!=='Contact Requested') {
return $(this).html('Contact Requested');
}
});
这是玉文件:
extends layout
block content
div
legend Search Results
div#userResults
for user in ufirstName
a(href='/user/#{user.id}')
p #{user.firstName} #{user.lastName}
button.addContact Add Contact