javascript - My simple routing is not working and I can't figure out why -
i'm working through basic node tutorial , having difficulty getting routes.js file work.
it working earlier today. server node reading file. reason, though not utilizing it. code looks tutorial -- though teacher on pc , on mac (though can't see why matter).
before issue started occur, hooked database (file below) -- again, can't see why screw routes. when put code in server.js, can proper routing.
help me stackoverflow, you're hope! see "cannot /"
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 run during every interaction; app.use(morgan('dev')); //sets req.cookies app.use(cookieparser()); app.use(session({ //secret user session secret: "ist0", //if server goes down, user can remain logged on -- still save database saveuninitialized: true, //even if nothing has changed, resave resave: true })); //routes require('./app/routes/routes'); // app.use('/', function(req, res){ // //send 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 exporting function (one expects app
argument):
module.exports = function(app) { ... }
but you're importing function , don't call it:
require('./app/routes/routes');
instead, need call , pass app
argument:
require('./app/routes/routes')(app);
Comments
Post a Comment