3
var mongoose = require('mongoose')
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/mydatabase'); //connect database

/* *
 * *    Define UserSchema
 * **/

var UserSchema = new Schema({
user_id : String,
email : String,
base_location : Number,
user_type : String,
number_of_event : Number
});

mongoose.model('User', UserSchema);
var User = mongoose.model('User');
var user  =new User


app.post('/api/users', function (req, res){
var product;
console.log("User: ");
console.log(req.body);
user = new ProductModel({
user_id: req.body.user_id,
email: req.body.email,
base_location: req.body.base_location,
});
product.save(function (err) {
if (!err) {
return console.log("created");
} else {
return console.log(err);
}
});
return res.send(user);
});

这是我的 app.js 它包含架构和发布功能我不知道如何在 html 中使用此文件我想制作可以插入用户数据的 indext.html 我该怎么做?3421

423

4

1 回答 1

1

There are a multitude a possibilities. What I usually do is create an express server and attach the routes to special functions in express.

//your modules
var express = require('express'),
    app = express(),
    mongoose = require('mongoose');

//connect mongo
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/mydatabase');

//schema
var UserSchema = new Schema({
 user_id : String,
 email : String,
 base_location : Number,
 user_type : String,
 number_of_event : Number
});

mongoose.model('User', UserSchema);
var User = mongoose.model('User');
var user  =new User

app.post('/api/users', function (req, res){
  //do your stuff
});

app.listen(80);

You then will need to run the above script (lets call it app.js) with

node app.js

If the code above is sane, this will run the server. When you connect to the server with the app then you will receive a connection. You should also look up some docs on socketIO.

于 2012-11-14T15:56:31.723 回答