Function.Module._load

时间:2018-09-04 09:17:40

标签: node.js express module node-modules

我遇到以下错误

  

Function.Module._resolveFilename(module.js:547:15)在Function.Module._load(module.js:474:25)在Module.require(module.js:596:17)在require(内部/ module.js:11:18)。 (.C:\ Users \ u8ser \ Desktop \ Todo \ server.js:25:1)在Module._compile(module.js:652:30)[nodemon]应用程序崩溃-等待文件更改后再启动

供您参考的PFB源代码。

我的server.js文件

var express = require("express");
var app = express();
var mongoose = require("mongoose");
var methodOverride = require("method-override");
var port = process.env.PORT || 8080;
var database = require('./config/database');
  var morgan = require("morgan");
var bodyParser = require("body-parser");

mongoose.connect('mongodb://localhost:27017', function(err){
    if (err){
        console.log(err);
    }else{
        console.log("Connected to DB");
    }
});

app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type : 'application/vnd.api+json'}));
app.use(methodOverride());

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

app.listen(port);
console.log("App listining to port" + port);

我的routes.js文件

var Todo = require('./models/todo');

module.exports = function(app){

    app.get('/api/todos', function(req, res){
    Todo.find(function(err, todos){
        if (err)
        res.send(err)

        res.send(todos)

      });
    });

 // create todo and send back all todos after creation

app.post('/api/todos', function(req, res){

    // create a todo, information comes from AJAX request from Angular

    Todo.create({
        text : req.body.text,
        done : false
    }, function(err, todo) {
        if (err)
           res.send(err);

       //get and return all the todos after you create another

       Todo.find(function(err, todos){
           if (err)
               res.send(err)
            res.json(todos);
       });
    });

});

//delete a todo
app.delete('/api/todos/:todo_id', function(req, res){
    Todo.remove({
       _id : req.params.todo_id
    }, function(err, todo){
        if (err)
        res.send(err);

        //get and return all the todos after you create another
        Todo.find(function(err, todos){
            if (err)
            res.send(err)
            res.json(todos);
        });
    });
});


// server.js 
// application -------------------------------------------------------------
app.get('*', function(req, res) {
    res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});

};

请访问此github link以获取完整的源代码。

2 个答案:

答案 0 :(得分:1)

问题出在server.js文件中。尝试以下更新的解决方案

Server.js:

var express = require("express");
var app = express();
var mongoose = require("mongoose");
var methodOverride = require("method-override");
var port = process.env.PORT || 8080;
var database = require('./config/database');
  var morgan = require("morgan");
var bodyParser = require("body-parser");
var routes = require('./app/models/routes');
mongoose.connect('mongodb://localhost:27017', function(err){
    if (err){
        console.log(err);
    }else{
        console.log("Connected to DB");
    }
});

app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(bodyParser.json({ type : 'application/vnd.api+json'}));
app.use(methodOverride());

app.use('/api', routes); // you should add base path like this and assign routes

app.listen(port);
console.log("App listining to port" + port);

在route.js文件中删除/ api,因为我们在server.js中将/ api添加为基本路径

var Todo = require('./todo');

module.exports = function(app){

    app.get('/todos', function(req, res){
    Todo.find(function(err, todos){
        if (err)
        res.send(err)

        res.send(todos)

      });
    });

 // create todo and send back all todos after creation

app.post('/todos', function(req, res){

    // create a todo, information comes from AJAX request from Angular

    Todo.create({
        text : req.body.text,
        done : false
    }, function(err, todo) {
        if (err)
           res.send(err);

       //get and return all the todos after you create another

       Todo.find(function(err, todos){
           if (err)
               res.send(err)
            res.json(todos);
       });
    });

});

//delete a todo
app.delete('/todos/:todo_id', function(req, res){
    Todo.remove({
       _id : req.params.todo_id
    }, function(err, todo){
        if (err)
        res.send(err);

        //get and return all the todos after you create another
        Todo.find(function(err, todos){
            if (err)
            res.send(err)
            res.json(todos);
        });
    });
});

// server.js 
// application -------------------------------------------------------------
app.get('*', function(req, res) {
    res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});

};

应该以下面的方式创建模式,但是您所做的是错误的

var mongoose = require('mongoose');
const Schema = mongoose.Schema;
const BlogPost = new Schema({
  text : String,
  done : Boolean
});
module.exports = mongoose.model('Todo', BlogPost);

答案 1 :(得分:1)

我认为您写了route.js文件的错误路径。 试试

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

代替

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