What is the url I put for the $.post calling to the server for a node.js file. I keep seeing tutorials with php files, but I am confused as to what node.js files to call? Am I suppose to post it to the app.js file or the route file?
This is what the app.js file looks like:
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);
});
This is what the route's file looks like: (I know the code in the route file might not be correct in trying to extract the user.id that is associated with the button, still a work in progress)
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'});
}
});
};
This is the script file, trying to figure out the URL:
$('.addContact').click(function() {
$.post('user.js',
function(data) {
$('.addContact').html(data);
}
);
if($(this).html!=='Contact Requested') {
return $(this).html('Contact Requested');
}
});
This is the Jade file:
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
NodeJS will run as a web server with its own url. When you launch the server you must specify a url . I guess most of the examples use some localhost address with a port. You can then post to that address. It should not be a php address unless you go via a php page on your own web server
The URL is entirely up to you. From your edited question, /addContact would be a valid path to use with $.post. I note that your code in the addContactPost function doesn't seem to render anything on success...