简体   繁体   English

我的简单路由无法正常工作,我不知道为什么

[英]My simple routing is not working and I can't figure out why

I'm working through a basic node tutorial and am having difficulty getting my routes.js file to work. 我正在完成基本节点教程,并且很难使我的route.js文件正常工作。

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). 我的代码看起来完全像本教程一样-尽管老师在PC上,而我在Mac上(尽管我看不出为什么会那么重要)。

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. 将这段代码放在server.js中时,可以获得正确的路由。

Help me stackoverflow, you're my only hope! 帮助我stackoverflow,您是我唯一的希望! All I see is "Cannot GET /" 我看到的只是“无法获取/”

My routes.js file 我的routes.js文件

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 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: 我的database.js文件:

module.exports = {
  'url': 'mongodb://localhost/meanboil'
}

You are exporting a function (one that expects app as an argument): 您正在导出一个函数(一个将app作为参数的函数):

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: 相反,您需要调用它并将app作为参数传递:

require('./app/routes/routes')(app);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法弄清楚为什么我添加事件处理程序的简单尝试无效 - I can't figure out why my simple attempt at adding an event handler isn't working 经典的元音练习。 我不知道为什么我的for循环无法正常工作 - classic vowel exercise. I can't figure out why my for loop isn't working 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 我不知道为什么我的石头,剪刀布游戏中的得分不起作用 - I can't figure out why the scoring isn't working in my rock, paper, scissors game 我的 javascript 验证刚刚停止工作,我不知道为什么 - My javascript validation just stopped working and I can't figure out why 编码新手,我不明白为什么我的 javascript 按钮无法正常工作? - New to coding,I can't figure out why my javascript buttons are not working properly? 我不知道我的 JavaScript 函数不起作用 - I can't figure out my JavaScript function is not working addEventListener()不起作用-无法找出原因 - addEventListener() Not Working - can't figure out why 无法弄清楚为什么循环不起作用 - Can't figure out why the loop is not working 我不知道为什么我的JavaScript无法正常工作…我正在将值动态传递给标签 - I can't figure out why my javascript isn't working… I'm dynamically passing values to a tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM