【问题标题】:Unable to figure out what's wrong. how to debug无法弄清楚出了什么问题。如何调试
【发布时间】:2018-05-09 11:46:31
【问题描述】:
module.js:471
    throw err;
    ^

Error: Cannot find module '..models/article'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\Sankalp Singhai\blogapp\server\routes\api.js
:4:17)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\Users\Sankalp Singhai\blogapp\server.js:7:13)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)

在哪里寻找模块文件夹?是数据库连接错误。在 mlab 上创建数据库。

【问题讨论】:

    标签: node.js mongodb angular crud


    【解决方案1】:
        const express = require('express');
        const bodyParser = require('body-parser');
        const path = require('path');
        const http = require('http');
        const app = express();
    
        const api = require('./server/routes/api');
        //parsers
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({extended : false}));
    
        app.use(express.static(path.join(__dirname,'dist')));
    
        app.use('/api' , api)
    
        app.get('*', (req,res)=>{
            res.sendFile(path.join(__dirname, 'dist/index.html'));
        });
    
        //set port
        const port = process.env.PORT || '3000';
        app.set('port' , port);
    
        const server = http.createServer(app);
    
        server.listen(port, () => console.log('Running on localhost: ${port}'));
    
    **server.js** 
    

    【讨论】:

      【解决方案2】:
      C:\Users\Sankalp Singhai\blogapp>node server.js
      (node:4068) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, us
      e `openUri()` instead, or set the `useMongoClient` option if using `connect()` o
      r `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mong
      o-client
      Running on localhost: ${port}
      Error connecting
      (node:4068) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejec
      tion id: 1): MongoError: failed to connect to server [6:27017] on first connect
      [MongoError: getaddrinfo ENOTFOUND 6 6:27017] 
      

      连接错误

      【讨论】:

      • 要使用模板字符串,你需要使用反引号“". It looks like you're using ${port}` 没有那个。另外,请不要反问。如果你愿意,你可以开始一个新的问题,或作为评论发布。在我们真正看到您的代码之前,我们无法调试此问题。
      • 您的 Mongo 实例是否正在运行?如果是,配置在哪里?你是如何连接到数据库的? MongoError 行应该告诉您它与您的 server.js 文件无关;检查你的 Mongo 配置。另外,将'Running on localhost: ${port}' 更改为`Running on localhost: ${port}`
      • 全部完成!感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 2017-08-28
      相关资源
      最近更新 更多