Last active
August 29, 2015 14:17
-
-
Save gabhi/ee291f92e8a5efb3fae0 to your computer and use it in GitHub Desktop.
rest server showcasing post/get/ getbyId using express/mongoose/mongodb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var app = express(); | |
| var request = require("request"); | |
| var bodyParser = require('body-parser') | |
| app.use(bodyParser.json()); // to support JSON-encoded bodies | |
| app.use(bodyParser.urlencoded({ // to support URL-encoded bodies | |
| extended: true | |
| })); | |
| var mongoose = require('mongoose'); | |
| mongoose.connect('mongodb://localhost/massdrop'); | |
| var JobSchema = new mongoose.Schema({ | |
| name: String, | |
| url: String, | |
| content: String, | |
| status: String, | |
| created_at: { | |
| type: Date | |
| }, | |
| updated_at: { | |
| type: Date | |
| } | |
| }); | |
| //Job Model | |
| var Job = mongoose.model('Job', JobSchema); | |
| JobSchema.pre('save', function(next) { | |
| now = new Date(); | |
| this.updated_at = now; | |
| if (!this.created_at) { | |
| this.created_at = now; | |
| } | |
| next(); | |
| }); | |
| var updateContents = function(id) { | |
| Job.findOne({ | |
| _id: id | |
| }, function(err, job) { | |
| request(job.url, function(error, response, body) { | |
| if (!error) { | |
| job.content = escape(body); | |
| job.status = "complete"; | |
| job.save(); | |
| } else { | |
| job.content = ""; | |
| job.status = error; | |
| job.save(); | |
| } | |
| }); | |
| }); | |
| } | |
| //API endpoint to check if server is up and running | |
| app.get('/api', function(req, res) { | |
| res.send({ | |
| status: 'running', | |
| _urls: { | |
| '/api/jobs': "get the list of jobs", | |
| "/api/jobs": "Create new Job", | |
| "/api/job/id": "get details of job" | |
| } | |
| }); | |
| }); | |
| //API to get list of all jobs | |
| app.get('/api/jobs', function(req, res) { | |
| Job.find(function(err, jobs) { | |
| if (err) return console.error(err); | |
| console.log(jobs); | |
| res.send(jobs); | |
| }) | |
| }); | |
| //API to get details about specific job | |
| app.get('/api/jobs/:id', function(req, res) { | |
| Job.findOne({ | |
| '_id': req.params.id | |
| }, function(err, jobs) { | |
| //send 400 status code if record not found | |
| if (err) return res.status(400).send("record doesn't exists"); | |
| console.log(jobs); | |
| res.send(jobs); | |
| }) | |
| }); | |
| //API to create a new job | |
| app.post('/api/job', function(req, res) { | |
| console.log(req.body.name); | |
| var fJob = new Job({ | |
| name: req.body.name, | |
| url: req.body.url, | |
| status: 'initiated' | |
| }); | |
| fJob.save(function(err) { | |
| if (err) console.log('done'); | |
| //Notice that post should return 201 as status code on successful creation of new resource | |
| res.status(201).send(fJob); | |
| updateContents(fJob._id); | |
| }); | |
| }); | |
| app.get('/', function(req, res) { | |
| res.send('Job Scheduler!') | |
| }) | |
| var server = app.listen(3000, function() { | |
| var host = server.address().address | |
| var port = server.address().port | |
| console.log('Example app listening at http://%s:%s', host, port) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment