Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var path = require('path');
- var bodyParser = require('body-parser');
- //connect to the mongo
- var db = require('mongoskin').db("mongodb://localhost:27017/test", { w: 0});
- db.bind('sites');
- //create the app instance
- var app = express();
- //serve static files
- app.use(express.static(path.join(__dirname, 'public')));
- //parse POST data
- app.use(bodyParser.urlencoded({ extended: false }));
- // // parse application/json
- app.use(bodyParser.json());
- function after_update(err, res, record){
- if (err){
- res.status(500);
- res.send({ error:err.toString() });
- } else {
- res.send(record || {});
- }
- }
- //data loading
- app.get('/data', function(req, res){
- db.record.find().toArray(function(err, data){
- for (var i = 0; i < data.length; i++){
- data[i].id = data[i]._id;
- delete data[i]._id;
- }
- res.send(data);
- });
- });
- app.post('/data', function(req, res){
- db.record.insert(req.body, function(err, record){
- if (err) return res.send({ status:"error" });
- res.send({ newid:req.body._id });
- });
- });
- app.put('/data/:id', function(req, res){
- db.record.updateById(req.param("id"), req.body, function(err){
- if (err) return res.send({ status:"error" });
- res.send({});
- });
- });
- app.delete('/data/:id', function(req, res){
- db.record.removeById(req.param("id"), req.body, function(err){
- if (err) return res.send({ status:"error" });
- res.send({});
- });
- });
- app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement