Full-Stack Web Development with Vue.js and Node

(singke) #1
Introducing REST APIs Chapter 4

const route = require("./controllers/" + file)
route.controller(app)
}
})

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development'? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;

app.listen(3000, function() {
console.log('listening on 3000')
})

Since this file is auto-generated when we build the application via command CLI, it uses


typescript syntax. If we want to use the ES 6 syntax, we can replace var with const.


In our models/User.js, we have the following:


const mongoose = require("mongoose")
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: String,
email: String
})

const User = mongoose.model("User", UserSchema)
module.exports = User
Free download pdf