2016-04-23 31 views
0

解决“localhost意外关闭的连接”错误我正在通过Chris Sevilleja的“Mean Machine:初学者的JavaScript栈实用指南”。我的问题在第60页。根据本书的说明,我创建了一个小的server.js文件以及package.json,并使用npm install来创建node_modules文件夹并使用适当的模块填充它。当我运行'server.js'时,我在终端上收到一条消息,该消息运行我放在程序结尾处的console.log()消息。所以我知道该程序运行。但是,当我再转到Chrome和在搜索栏中把'https://localhost:8080“,我收到以下错误信息:在节点

这个网站无法达到

本地主机意外关闭了连接

ERR_CONNECTION_CLOSED

我花了一个多小时试图弄清楚了这一点,我很丢失。如果我使用“HTTP”,而不是“https”时,它只是需要永远载入(我等了5分钟)。我有ch ecked我是否在Chrome中拥有异步DNS标志或确认它已启用。我试着做 “$ lsof的-i:8080” 在终端上,并得到:

COMMAND PID USER FD类型的设备尺寸/关闭节点> NAME 节点6188 23U的IPv6 0x3a96cd0448d7ba87 0t0 TCP> *:HTTP-ALT( LISTEN)

(不能确定它做什么,但我看到了它在网页上解决类似的问题和应答指示的人做到这一点。)我试图访问它在Safari以及和接收相同信息。

我的代码如下: '(!欢迎到主页')

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

var port = process.env.PORT || 8080; // set the port for our app 

var app = express(); // define our app in terms of Express 
app.use(bodyParser.urlencoded({extended: true})); 
app.use(bodyParser.json); 

// configure out app to handle CORS requests 
app.use(function(req, res, next) { 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST'); 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization'); 
    next(); 
}); 

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

// API routes 

// basic route for the home page 
// route, not middleware, so no 'next' parameter 
app.get('/', function(req, res) { 
    res.send('Welcome to the home page!'); 
}); 

// get an instance of the express router 
var apiRouter = express.Router(); 

// test route to make sure everything is working 
// accessed at GET http://localhost:8080/api 
apiRouter.get('/', function(req, res) { 
    res.json({message: 'Welcome to our API.'}); 
}); 

// mount apiRouter on our app 
// they will all be prefixed with /api 
app.use('/api', apiRouter); 

// start the server on the port we indicated on line 6 
app.listen(port); 
console.log('Magic happens on port ' + port); 

为让我res.send '任何建议;'要显示的消息将是太棒了。提前致谢。我尽可能地描述了这个问题,但如果有任何其他信息需要请告诉我。

后续排序:我正在使用SublimeText。使用更复杂的IDE可以识别/解决这个问题吗?

+1

欢迎Programmers.SE!您可能想查看:[我可以在这里询问哪些主题?](http://programmers.stackexchange.com/help/on-topic) –

+0

启动应用程序后,控制台中是否有任何错误? –

+0

您是否确认在提出请求时程序仍在运行? – CodesInChaos

回答