Skip to content

Commit

Permalink
building a RESTful API
Browse files Browse the repository at this point in the history
  • Loading branch information
liccy-fuentes committed Oct 27, 2014
0 parents commit d969c77
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DS_Store
node_modules
bower_components
nbproject
21 changes: 21 additions & 0 deletions models/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Dependencies
var restful = require('node-restful');
var mongoose = restful.mongoose;

//Schema
var productSchema = new mongoose.Schema({
courses:[{
name:String,
courseid: Number,
status: String,
sections:[{
name: String,
sectionid: Number,
sectioncode: Number,
status: String
}]
}]
});

//Return model
module.exports = restful.model('Products', productSchema);
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "RESTful-API",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/the-creature/RESTful-API.git"
},
"author": "Liccy Fuentes",
"license": "ISC",
"bugs": {
"url": "https://github.com/the-creature/RESTful-API/issues"
},
"homepage": "https://github.com/the-creature/RESTful-API",
"dependencies": {
"express": "^4.10.0",
"mongoose": "^3.8.18",
"node-restful": "^0.1.17",
"body-parser": "^1.9.1",
"nodemon": "^1.2.1",
"mongodb": "^1.4.19"
}
}
13 changes: 13 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//Dependencies
var express = require('express');
var router = express.Router();

//Models
var Product = require('../models/product');

//Routes
Product.methods(['get', 'put', 'post', 'delete']);
Product.register(router, '/products');

//Return Router
module.exports = router;
20 changes: 20 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//Dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

//MongoDB
mongoose.connect('mongodb://localhost/rest_test');


//Express
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

//Routes
app.use('/api', require('./routes/api'));

//Start server
app.listen(3000);
console.log('API is working on port 3000');

0 comments on commit d969c77

Please sign in to comment.