I do REST API on node.js + express.js I have 2 questions at once.
server.js var express = require('express'); var users = require('./routes/users'); var app = express(); //Routes app.use('/users', users); _______________________________________ /routes/users.js var express = require('express'); var user = require('../app/models/user'); var router = express.Router(); router.get('/users', function (req, res) { models.User.findAll() .then(function (users) { res.json(users); }) }); module.exports = router; ______________________________________ /models/user.js module.exports = function(sequelize, Data) { var User = sequelize.define('User', { firstname: { type: Sequelize.STRING }, password: { type: Sequelize.STRING }, role: { type: Sequelize.STRING } }); return User; } The first question is an error when I make a request to http: // localhost: 3000 / users / users
ReferenceError: models is not defined at /home/sergei/Документы/NodeJSProjects/ParkingHouse/routes/users.js:7:5 GET /users/users 500 And the second question is how to change the routing to refer to the addresses:
http: // localhost: 3000 / api / users link to /routes/users.js
http: // localhost: 3000 / api / cars link to /routes/cars.js
I have not tried it of course, but I believe that this option will not work
//Routes app.use('/api', users); app.use('/api', cars);
/routes/users.js, themodelsvariable is not defined, hence the error. 2. Option withapp.use('/api', users);will work - Dmitriy Simushev