hi,ii am new with nodejs.i habe worked on an utube-example from Brad Traversy, bookstore. I would like to source the rest.functions out from app.js (see snippet below) into stand-alone files like genreRest.js, bookRest.js, or further (customer, store,..). i tried with module.exports und require but obviously i dint manage. Can anybody give me a hint how to extract the rest-functions from app.js into standalone js-files, for example the genre-REstfunctions below?thanx a lot for helping.dukehh
app.js:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
app.use(bodyParser.json());
Genre = require('./models/genre');
Book = require('./models/book');
//Connect to mongoose
mongoose.connect('mongodb://localhost/bookstore');
var db = mongoose.connection;
app.get('/', function(req, res){
res.send('Please use /api/books or /api/genres');
});
// Get Genres
app.get('/api/genres', function(req, res){
Genre.getGenres(function (err, genres) {
// body...
if(err){
throw err;
}
res.json(genres);
});
});
// Add Genre
...
// Get Books
app.get('/api/books', function(req, res){
Book.getBooks(function (err, books) {
// body...
if(err){
throw err;
}
res.json(books);
});
});
// Get Book by ID
...
// Add Books
...
app.listen(3000);
...