2016-11-24 107 views
0

在我的應用程序,當我嘗試打電話給我收到錯誤像這樣的服務:`npm`錯誤爲'錯誤:無法找到module` - 如何解決

module.js:327 
    throw err; 
    ^

Error: Cannot find module '/api/sessions' 
    at Function.Module._resolveFilename (module.js:325:15) 
    at Function.Module._load (module.js:276:25) 
    at Module.require (module.js:353:17) 
    at require (internal/module.js:12:17) 
    at Object.<anonymous> (C:\Tutorials\try\ModernWebApp\server.js:13:9) 
    at Module._compile (module.js:409:26) 
    at Object.Module._extensions..js (module.js:416:10) 
    at Module.load (module.js:343:32) 
    at Function.Module._load (module.js:300:12) 
    at Function.Module.runMain (module.js:441:10) 
    at startup (node.js:139:18) 
    at node.js:968:3 
[14:38:53] [nodemon] app crashed - waiting for file changes before starting... 

這裏是我的server.js文件:

var express = require('express'); 
var bodyParser = require('body-parser'); 


var app = express(); 

app.use(express.static(__dirname + '/assets')) 

app.use(express.static(__dirname + '/templates')) 

app.use(bodyParser.json()); 
//if i remove both this 2 lines, no issues come. 
app.use(require('/api/sessions', require(__dirname + '/controllers/api/sessions'))); 
app.use(require('/api/users', require('./controllers/api/users'))); 



app.get('/', function(req, res){ 
    res.sendFile(__dirname + '/layouts/app.html'); 
}) 

app.listen(8888, function(){ 
    console.log('app listening!' + 8888); 
}) 

這裏有什麼問題,如何解決這個問題。這兩個文件無非是從前端處理查詢。

session.js樣品:只使用

var router  = require('express').Router(); 
var bcrypt  = require('bcryptjs'); 
var jwt   = require('jwt-simple'); 
var User  = require('../../models/user'); 
var config  = require('../../config'); 


router.post('/sessions', function(req, res, next) { 

    User.findOne({ 
     username: req.body.username, 
    }).select('password').select('username') 
    .exec(function(err, user) { 
     if(err) { return next(err)} 

     if(!user) { return res.send(401)} 

     bcrypt.compare(req.body.password, user.password, function(err, valid) { 

      if(err) { return next(err)} 

      if(!valid) { return res.send(401)} 

      var token = jwt.encode({username:user.username}, config.secret); 

      res.send(token); 

     }) 


    }) 

}); 

module.exports = router; 

回答

1

你只需要使用require('/api/sessions'),所以節點將尋找sessions.js文件在您root dir(/)不是你的工作目錄。

也許你應該試試require('./api/sessions')

相關問題