2017-03-07 65 views
1

我有兩個app.listens在代碼中,我得到一個錯誤,當我把他們的一出,認證停止工作,我怎麼能解決這個問題,我想實現的護照JS成我的應用程序(我按照一個教程,我有護照JS例如工作,我只是想實現它MYPROJECT),這是下面的錯誤,我越來越錯誤:聽EADDRINUSE 3443

screenshot here

'use strict' 

const express = require('express') 
const fs = require('fs') 
const https =require('https') 
const path = require('path') 

var app   = express(); 

var bodyParser = require('body-parser'); 
var morgan  = require('morgan'); 
var mongoose = require('mongoose'); 

var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens 
var config = require('./config'); // get our config file 
var Index = require('./api/planner/index'); // get our mongoose model 


var port = process.env.PORT || 3443; // used to create, sign, and verify tokens 
mongoose.connect(config.database); // connect to database 
app.set('superSecret', config.secret); // secret variable 

// use body parser so we can get info from POST and/or URL parameters 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(bodyParser.json()); 

// use morgan to log requests to the console 
app.use(morgan('dev')); 

// ======================= 
// routes ================ 
// ======================= 
// basic route 
app.get('/', function(req, res) { 
    res.send('Hello! The API is at http://localhost:' + port + '/api'); 
}); 

// API ROUTES ------------------- 
// get an instance of the router for api routes 
var apiRoutes = express.Router(); 

// TODO: route to authenticate a user (POST http://localhost:8080/api/authenticate) 
apiRoutes.post('/authenticate', function(req, res) { 
// find the user 
    User.findOne({ 
    name: req.body.name 
    }, function(err, user) { 

    if (err) throw err; 

    if (!user) { 
     res.json({ success: false, message: 'Authentication failed. User not found.' }); 
    } else if (user) { 

     // check if password matches 
     if (user.password != req.body.password) { 
     res.json({ success: false, message: 'Authentication failed. Wrong password.' }); 
     } else { 

     // if user is found and password is right 
     // create a token 
     var token = jwt.sign(user, app.get('superSecret'), { 
      expiresIn: 1440 // expires in 24 hours 
     }); 

     // return the information including token as JSON 
     res.json({ 
      success: true, 
      message: 'Enjoy your token!', 
      token: token 
     }); 
     } 

    } 

    }); 
}); 
// TODO: route middleware to verify a token 
apiRoutes.use(function(req, res, next) { 

    // check header or url parameters or post parameters for token 
    var token = req.body.token || req.query.token || req.headers['x-access-token']; 

    // decode token 
    if (token) { 

    // verifies secret and checks exp 
    jwt.verify(token, app.get('superSecret'), function(err, decoded) {  
     if (err) { 
     return res.json({ success: false, message: 'Failed to authenticate token.' });  
     } else { 
     // if everything is good, save to request for use in other routes 
     req.decoded = decoded;  
     next(); 
     } 
    }); 

    } else { 

    // if there is no token 
    // return an error 
    return res.status(403).send({ 
     success: false, 
     message: 'No token provided.' 
    }); 

    } 
}); 

// route to show a random message (GET http://localhost:8080/api/) 
apiRoutes.get('/', function(req, res) { 
    res.json({ message: 'Welcome to the coolest API on earth!' }); 
}); 

// route to return all users (GET http://localhost:8080/api/users) 
apiRoutes.get('/users', function(req, res) { 
    User.find({}, function(err, users) { 
    res.json(users); 
    }); 
}); 

// apply the routes to our application with the prefix /api 
app.use('/api', apiRoutes); 
// we'll get to these in a second 
app.get('/setup', function(req, res) { 

    // create a sample user 
    var nick = new User({ 
    name: 'Nick Cerminara', 
    password: 'password', 
    admin: true 
    }); 

    // save the sample user 
    nick.save(function(err) { 
    if (err) throw err; 

    console.log('User saved successfully'); 
    res.json({ success: true }); 
    }); 
}); 



// ======================= 
// start the server ====== 
// ======================= 
app.listen(port); 
console.log('Magic happens at http://localhost:' + port); 


const directoryToServe = 'client' 
//const port = 3443 

app.use('/',express.static(path.join(__dirname,'..',directoryToServe))) 

const httpsOptions = { 
    cert: fs.readFileSync(path.join(__dirname,'ssl','server.crt')), 
    key: fs.readFileSync(path.join(__dirname,'ssl','server.key')) 
} 

https.createServer(httpsOptions, app) 
.listen(port, function() 
{ 
    console.log(`Serving the ${directoryToServe}/directory at https://localhost:${port}`)}) 

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






app.use(bodyParser.json()); 
app 
app.get('/', function(request, response) { 
    response.writeHead(200, {"Content-Type": "text/plain"}); 
    response.end("We're up and running!!!"); 
}); 


var plan = require('./api/planner/index'); 

app.get('/api/planner',plan.index); 
app.post('/api/planner',plan.create); 
app.put('/api/planner/:id',plan.update); 
app.delete('/api/planner/:id',plan.delete); 




console.log("Server running at http://127.0.0.1:8000/"); 
// 
+0

你使用windows嗎?你打開了多少個命令窗口? –

+0

是,一,它是無關的其他窗口,當我拿出'app.listen(端口);',我沒有得到任何錯誤,但隨後本地主機不工作 – Russkiy

+0

此端口由另一個。取另一個端口號 –

回答

1

這ERR可能是造成通過使用一個繁忙的端口。如果你使用的是Ubuntu,你可以通過lsof -i:portnumber來檢查端口的狀態。運行命令後,您將擁有一個PID。 您可以通過kill -9 pid發佈端口。

Windows和Mac也有類似的命令。

+0

雖然Windows沒有'lsof'。 – tadman

+0

窗戶情況下,你可以看看這個帖子 - http://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-port-on - 窗口 – TheGiantBeast

+0

感謝您的額外信息。 @tadman – zhangjinzhou

相關問題