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);
});
this is my app.js it contains schema and post function i don't know how can i use this file in html i want to make indext.html which can insert user data how can i do that? 3421
423
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.