I'm working through a basic node tutorial and am having difficulty getting my routes.js file to work.
It was working earlier today. The server Node is reading the file. For some reason, though it is not utilizing it. My code looks exactly like the tutorial -- though the teacher is on a PC and I am on a Mac (though I can't see why that would matter).
Before this issue started to occur, I hooked up my database (file below) -- but again, I can't see why that would screw with my routes. When I put this code in server.js, I can get the proper routing.
Help me stackoverflow, you're my only hope! All I see is "Cannot GET /"
My routes.js file
var User = require('../models/user');
module.exports = function(app){
app.get('/', function(req, res){
res.send("Hello World");
});
// app.get('/:username/:password', function(req, res){
// var newUser = new User();
// newUser.local.username = req.params.username;
// newUser.local.password = req.params.password;
// console.log(newUser.local.username + " " + newUser.local.password);
// newUser.save(function(err){
// if(err)
// throw err;
// });
// res.send('Success!')
// });
};
server.js
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var cookieParser = require('cookie-parser');
var session = require('express-session');
var morgan = require('morgan');
var mongoose = require('mongoose');
//Config Database
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
//MIDDLEWARE is run during every interaction;
app.use(morgan('dev'));
//sets req.cookies
app.use(cookieParser());
app.use(session({
//secret for user session
secret: "ist0",
//if the server goes down, the user can remain logged on -- still save to database
saveUninitialized: true,
//even if nothing has changed, resave
resave: true
}));
//ROUTES
require('./app/routes/routes');
// app.use('/', function(req, res){
// //send is express function
// res.send("Our first express program");
// console.log(req.cookies);
// console.log("============");
// console.log(req.session);
// });
app.listen(port);
console.log('The magic happens on ' + port)
My database.js file:
module.exports = {
'url': 'mongodb://localhost/meanboil'
}
You are exporting a function (one that expects app
as an argument):
module.exports = function(app) { ... }
But you're just importing that function and don't actually call it:
require('./app/routes/routes');
Instead, you need to call it and pass app
as argument:
require('./app/routes/routes')(app);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.