on
Italy
- Get link
- X
- Other Apps
1
2
3
4
5
6
7
8
| { name: library-rest-api, version:0.0.1, description:A simple library REST api, dependencies: { express:~3.1.0 }} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| // Module dependencies.var application_root = __dirname, express = require( 'express' ); //Web framework//Create servervar app = express();// Configure serverapp.configure( function() { //parses request body and populates request.body app.use( express.bodyParser() ); //checks request.body for HTTP method overrides app.use( express.methodOverride() ); //perform route lookup based on url and HTTP method app.use( app.router ); //Show all errors in development app.use( express.errorHandler({ dumpExceptions: true, showStack: true }));});//Router//Get a list of all booksapp.get( '/api/books', function( request, response ) { var books = [ { title: "Book 1", author: "Author 1", releaseDate: "01/01/2014" }, { title: "Book 2", author: "Author 2", releaseDate: "02/02/2014" } ]; response.send(books);});//Insert a new bookapp.post( '/api/books', function( request, response ) { var book = { title: request.body.title, author: request.body.author, releaseDate: request.body.releaseDate }; response.send(book);});//Get a single book by idapp.get( '/api/books/:id', function( request, response ) { var book = { title: "Unique Book", author: "Unique Author", releaseDate: "03/03/2014" }; response.send(book);});//Update a bookapp.put( '/api/books/:id', function( request, response ) { response.send("Updated!");});//Delete a bookapp.delete( '/api/books/:id', function( request, response ) { response.send("Deleted");});//Start servervar port = 4711;app.listen( port, function() { console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );}); |
node server.js
1
2
3
4
5
6
7
8
9
| { "name": "library-rest-api", "version": "0.0.1", "description": "A simple library REST api", "dependencies": { "express": "~3.1.0", "mongoose": "~3.5.5" }} |
npm install
1
2
3
4
5
| // Module dependencies.var application_root = __dirname,express = require( 'express' ), //Web frameworkpath = require( 'path' ), //Utilities for dealing with file pathsmongoose = require( 'mongoose' ); //Used for accessing a MongoDB database |
//Connect to database
mongoose.connect( 'mongodb://localhost/library_database' );
//Schema
var BookSchema = new mongoose.Schema({
title: String,
author: String,
releaseDate: Date
});
//Model
var BookModel = mongoose.model( 'Book', BookSchema );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| //Router//Get a list of all booksapp.get( '/api/books', function( request, response ) {return BookModel.find(function( err, books ) {if( !err ) {return response.send( books );} else {console.log( err );return response.send('ERROR');}});});//Insert a new bookapp.post( '/api/books', function( request, response ) {var book = new BookModel({title: request.body.title,author: request.body.author,releaseDate: request.body.releaseDate});console.log(request.body.title);book.save( function( err ) {if( !err ) {console.log( 'created' );return response.send( book );} else {console.log( err );return response.send('ERROR');}});});//Get a single book by idapp.get( '/api/books/:id', function( request, response ) {return BookModel.findById( request.params.id, function( err, book ) {if( !err ) {return response.send( book );} else {console.log( err );return response.send('ERROR');}});});//Update a bookapp.put( '/api/books/:id', function( request, response ) {return BookModel.findById( request.params.id, function( err, book ) {book.title = request.body.title;book.author = request.body.author;book.releaseDate = request.body.releaseDate;return book.save( function( err ) {if( !err ) {console.log( 'book updated' );return response.send( book );} else {console.log( err );return response.send('ERROR');}});});});//Delete a bookapp.delete( '/api/books/:id', function( request, response ) {BookModel.findById( request.params.id, function( err, book ) {return book.remove( function( err ) {if( !err ) {console.log( 'Book removed' );return response.send( '' );} else {console.log( err );return response.send('ERROR');}});});}); |

Comments
Post a Comment