请问大家!nodejs成功启动了,但是localhost没有响应,也就是打不开网页?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问大家!nodejs成功启动了,但是localhost没有响应,也就是打不开网页?相关的知识,希望对你有一定的参考价值。

亲问一下各位!nodejs成功启动了,打开浏览器访问localhost时一直访问不了,总是在等待localhost响应,端口号也没有问题,以前是没有问题的,现在不行了!请帮帮忙,不胜感激

参考技术A ping localhost,看返回的是不是127.0.0.1,或者直接把localhost换成127.0.0.1

NodeJS 应用程序构建成功(Heroku),但启动时 Heroku 中的应用程序错误

【中文标题】NodeJS 应用程序构建成功(Heroku),但启动时 Heroku 中的应用程序错误【英文标题】:NodeJS app build is successful (Heroku) but Application Error in heroku at the launch 【发布时间】:2018-04-07 19:16:03 【问题描述】:

我正在使用 Cloud 9 IDE。 当我这样做时构建成功

git add .
git commit -m "first_commit"
git push heroku master

结果是这样的:

    Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote: 
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=true
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote: 
remote: -----> Installing binaries
remote:        engines.node (package.json):  4.7.3
remote:        engines.npm (package.json):   2.15.11
remote:        
remote:        Resolving node version 4.7.3...
remote:        Downloading and installing node 4.7.3...
remote:        npm 2.15.11 already installed with node
remote: 
remote: -----> Restoring cache
remote:        Loading 2 from cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (not cached - skipping)
remote: 
remote: -----> Building dependencies
remote:        Installing node modules (package.json)
remote: 
remote: -----> Caching build
remote:        Clearing previous node cache
remote:        Saving 2 cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (nothing to cache)
remote: 
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote:        Procfile declares types     -> (none)
remote:        Default types for buildpack -> web
remote: 
remote: -----> Compressing...
remote:        Done: 13.8M
remote: -----> Launching...
remote:        Released v5
remote:        https://still-reef-69131.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/still-reef-69131.git
   c263d3a..ad74a53  master -> master

这是我的 app.js

var express = require("express") ;
var app = express();

app.use(express.static("public")) ;

var sanitizer = require("express-sanitizer") ;

var methodOverride = require("method-override") ;
app.use(methodOverride("_method")) ;

var bodyparser = require("body-parser") ;
app.use(bodyparser.urlencoded(extended :true)) ;

app.use(sanitizer()) ;
var mongoose = require("mongoose") ;
mongoose.connect("mongodb://localhost/restful_blog") ;

var blogSchema = new mongoose.Schema(
    title: String,
    image: String,
    body: String,
    date: type: Date, default: Date.now
);
var blog = mongoose.model("blog", blogSchema);

app.get("/", function(req,res)
    res.redirect("/blogs") ;
) ;

app.get("/blogs" , function(req,res)
    blog.find(,function(err , maal)
        if(err) console.log(err) ;
        else res.render("index.ejs",blogs : maal) ;
    ) ;
) ;

app.get("/blogs/new" , function(req,res)
    res.render("new.ejs") ;
) ;

app.post("/blogs" , function(req,res)
    req.body.body = req.sanitize(req.body.body) ;
    blog.create(title : req.body.title , body : req.body.body , image : req.body.image , function(err,newblog)
        if(err) console.log(err) ;
        else    res.redirect("/blogs") ;
    ) ;
) ;

app.get("/blogs/:id" , function(req,res)
    blog.findById(req.params.id , function(err,maal)
        if(err) console.log(err) ;
        else 
            res.render("show.ejs" , readmore : maal) ;
        
    ) ;
) ;

app.get("/blogs/:id/edit" , function(req,res)
    blog.findById(req.params.id , function(err,maal)
        if(err) console.log(err) ;
        else 
            res.render("edit.ejs", blog:maal ) ;
        
    ) ;
) ;

app.put("/blogs/:id/edit" , function(req,res)
    req.body.body = req.sanitize(req.body.body) ;
    blog.findByIdAndUpdate(req.params.id , req.body , function(err,maal)
        if(err) console.log(err) ;
        else 
             res.redirect("/blogs/" + maal._id) ; 
    ) ;
) ;

app.delete("/blogs/:id" , function(req,res)
    blog.findByIdAndRemove(req.params.id , function(err)    // No maal here as there is nothing 
        if(err) console.log(err) ;
        else res.redirect("/blogs") ;
    ) ;
) ;

app.listen(process.env.PORT , process.env.IP , function()
    console.log("Server is ON") ;
) ;  // Server start

这是 package.json


  "name": "restful_blog_app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": 
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  ,
  "engines": 
    "node": "4.7.3" ,
    "npm": "2.15.11"
  ,
  "author": "MeMyself&I",
  "license": "ISC",
  "dependencies": 
    "body-parser": "^1.17.2",
    "ejs": "^2.5.6",
    "express": "^4.15.3",
    "express-sanitizer": "^1.0.2",
    "method-override": "^2.3.9",
    "mongoose": "^4.11.1"
  

但在启动应用程序时仍然存在应用程序错误:

应用程序错误: 应用程序发生错误,无法提供您的页面。如果您是应用程序所有者,请查看您的日志以了解详细信息。

Heroku 日志:

2017-10-26T11:41:48.495327+00:00 app[web.1]: 
2017-10-26T11:41:48.495329+00:00 app[web.1]: events.js:141
2017-10-26T11:41:48.495330+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2017-10-26T11:41:48.495330+00:00 app[web.1]:       ^
2017-10-26T11:41:48.496369+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
2017-10-26T11:41:48.496373+00:00 app[web.1]:     at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:280:12)
2017-10-26T11:41:48.496371+00:00 app[web.1]:     at null.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:336:35)
2017-10-26T11:41:48.496371+00:00 app[web.1]:     at emitOne (events.js:77:13)
2017-10-26T11:41:48.496372+00:00 app[web.1]:     at emit (events.js:169:7)
2017-10-26T11:41:48.496373+00:00 app[web.1]:     at emitTwo (events.js:87:13)
2017-10-26T11:41:48.496373+00:00 app[web.1]:     at g (events.js:260:16)
2017-10-26T11:41:48.496374+00:00 app[web.1]:     at emit (events.js:172:7)
2017-10-26T11:41:48.496374+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:187:49)
2017-10-26T11:41:48.496375+00:00 app[web.1]:     at Socket.g (events.js:260:16)
2017-10-26T11:41:48.496375+00:00 app[web.1]:     at emitOne (events.js:77:13)
2017-10-26T11:41:48.496376+00:00 app[web.1]:     at Socket.emit (events.js:169:7)
2017-10-26T11:41:48.496376+00:00 app[web.1]:     at emitErrorNT (net.js:1269:8)
2017-10-26T11:41:48.496377+00:00 app[web.1]:     at nextTickCallbackWith2Args (node.js:458:9)
2017-10-26T11:41:48.496377+00:00 app[web.1]:     at process._tickCallback (node.js:372:17)
2017-10-26T11:41:48.520747+00:00 app[web.1]: npm ERR! Linux 3.13.0-133-generic
2017-10-26T11:41:48.522269+00:00 app[web.1]: npm ERR! npm  v2.15.11
2017-10-26T11:41:48.510469+00:00 app[web.1]: 
2017-10-26T11:41:48.521254+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2017-10-26T11:41:48.521574+00:00 app[web.1]: npm ERR! node v4.7.3
2017-10-26T11:41:48.522544+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-10-26T11:41:48.522760+00:00 app[web.1]: npm ERR! restful_blog_app@1.0.0 start: `node app.js`
2017-10-26T11:41:48.523180+00:00 app[web.1]: npm ERR! 
2017-10-26T11:41:48.522963+00:00 app[web.1]: npm ERR! Exit status 1
2017-10-26T11:41:48.523387+00:00 app[web.1]: npm ERR! Failed at the restful_blog_app@1.0.0 start script 'node app.js'.
2017-10-26T11:41:48.523612+00:00 app[web.1]: npm ERR! This is most likely a problem with the restful_blog_app package,
2017-10-26T11:41:48.523824+00:00 app[web.1]: npm ERR! not with npm itself.
2017-10-26T11:41:48.524012+00:00 app[web.1]: npm ERR! Tell the author that this fails on your system:
2017-10-26T11:41:48.524213+00:00 app[web.1]: npm ERR!     node app.js
2017-10-26T11:41:48.524422+00:00 app[web.1]: npm ERR! You can get information on how to open an issue for this project with:
2017-10-26T11:41:48.524630+00:00 app[web.1]: npm ERR!     npm bugs restful_blog_app
2017-10-26T11:41:48.525228+00:00 app[web.1]: npm ERR!     npm owner ls restful_blog_app
2017-10-26T11:41:48.524834+00:00 app[web.1]: npm ERR! Or if that isn't available, you can get their info via:
2017-10-26T11:41:48.525418+00:00 app[web.1]: npm ERR! There is likely additional logging output above.
2017-10-26T11:41:48.525037+00:00 app[web.1]: npm ERR! 
2017-10-26T11:41:48.530341+00:00 app[web.1]: 
2017-10-26T11:41:48.530616+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2017-10-26T11:41:48.530813+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2017-10-26T11:41:48.620147+00:00 heroku[web.1]: State changed from starting to crashed
2017-10-26T11:41:48.607867+00:00 heroku[web.1]: Process exited with status 1
2017-10-26T11:42:04.141504+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=still-reef-69131.herokuapp.com request_id=525c4f82-20fc-4d02-9b21-848c0011e451 fwd="115.97.201.36" dyno= connect= service= status=503 bytes= protocol=https
2017-10-26T11:42:05.503594+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=still-reef-69131.herokuapp.com request_id=648fd443-3661-49d1-87f7-7ba27c065dff fwd="115.97.201.36" dyno= connect= service= status=503 bytes= protocol=https
2017-10-26T11:43:56.405496+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=still-reef-69131.herokuapp.com request_id=6e94416f-ac28-4196-8ecd-7f701e96785e fwd="115.97.201.36" dyno= connect= service= status=503 bytes= protocol=https
2017-10-26T11:43:57.089498+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=still-reef-69131.herokuapp.com request_id=b2069968-30b5-4875-9b04-ad3fa5c38419 fwd="115.97.201.36" dyno= connect= service= status=503 bytes= protocol=https

任何想法我哪里出错了??

【问题讨论】:

【参考方案1】:

问题在于您的猫鼬连接。

你安装 heroku mongodb 插件了吗?

$heroku addons:create mongolab

有关在 heroku 上使用 mongodb 的更多信息,请参见此处。 nodejs-mongoose

【讨论】:

哦.. 我明白了。我忘了在这里使用 mongolab。非常感谢

以上是关于请问大家!nodejs成功启动了,但是localhost没有响应,也就是打不开网页?的主要内容,如果未能解决你的问题,请参考以下文章

一个目录下多个nodejs项目

NodeJs - 检查子进程是不是成功启动

最近Android studio出了1.0版,但是SDK一直无法下载,请问大家是怎么解决的?

已经安装JDK,并且环境配置是正确的,但打开JDK一直跳出重新安装软件,请问怎么解决?

vue2启动成功了,但是无法访问

我在windows 2003服务器中设置了任务计划,执行一个bat。但是没有执行,请问是设定的问题吗,请大家帮助