简体   繁体   English

节点找不到模块-Express.js

[英]Node can't find the module - Express.js

I have ASP.NET background. 我有ASP.NET背景。 I'm trying to understand the "route" logic of Node.js using MVC logic in ASP.NET. 我正在尝试使用ASP.NET中的MVC逻辑来了解Node.js的“路由”逻辑。 For example in my global.js, there is an AJAX call to /users/userlist (I don't know what it is, it's controller in MVC). 例如,在我的global.js中,有一个对/ users / userlist的AJAX调用(我不知道它是什么,它是MVC中的控制器)。 I'm trying to reach that controller using below code, but I get this : 我正在尝试使用以下代码到达该控制器,但我得到了:

GET http://localhost:3000/users/userlist 404 (Not Found) GET http:// localhost:3000 / users / userlist 404(未找到)

What should I do to get rid of this? 我应该怎么做才能摆脱这种情况? Thanks. 谢谢。

This is app.js (complete) : 这是app.js(完整):

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest2');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(function(req,res,next){
    req.db = db;
    next();
});


app.use('/', index);
app.use('/userlist', users);

// 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;

This is users.js (complete) : 这是users.js(完整):

var express = require('express');
var router = express.Router();

/*
 * GET userlist.
 */
router.get('/userlist', function(req, res) {
    var db = req.db;
    var collection = db.get('userlist');
    collection.find({},{},function(e,docs){
        res.json(docs);
    });
});

module.exports = router;

This is global.js : 这是global.js:

$.getJSON( '/users/userlist', function( data ) {

    // For each item in our JSON, add a table row and cells to the content string
    $.each(data, function(){
        tableContent += '<tr>';
        tableContent += '<td><a href="#" class="linkshowuser" rel="' + this.username + '">' + this.username + '</a></td>';
        tableContent += '</tr>';
    });

    // Inject the whole content string into our existing HTML table
    $('#userList table tbody').html(tableContent);
});

This is index.js : 这是index.js:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

When I run this code, I get this : 当我运行这段代码时,我得到了:

GET http://localhost:3000/users/userlist 404 (Not Found) GET http:// localhost:3000 / users / userlist 404(未找到)

Inlucde the following line all code i app.js 在app.js的所有代码中添加以下行

var express = require('express');
var app = express();
// other code below

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM