简体   繁体   English

Node.JS不会释放localhost

[英]Node.JS will not release localhost

I was building another application in node locally and have now deployed it and am working on another application. 我正在本地节点中构建另一个应用程序,现在已经部署了它,并且正在开发另一个应用程序。

EDIT 编辑

However whenever I start node (v5) with express(v4.13) on my localhost it will just hang and not make any connections I am also on a Mac running El Capitan. 但是,每当我在本地主机上用express(v4.13)启动节点(v5)时,它都将挂起并且不建立任何连接,我也在运行El Capitan的Mac上。 All that I ever see in the console (By Console I mean the Terminal via Logging) is: 我在控制台中看到的所有内容(通过控制台,我是指通过日志记录显示的终端)是:

GET / -- ms --

Here is my code below for guidance. 这是我的以下代码,以供参考。

var express         = require('express');
var fs              = require('fs');
var logger          = require('morgan');
var cookieParser    = require('cookie-parser');
var bodyParer       = require('body-parser');
var path            = require('path');
var app             = express();
var settings        = app.settings;
var env             = process.env;
var entrance        = require('./route/siteBase');

app.set('view engine', 'jade');
app.set('view cache', false);

app.set('views', __dirname + '/source');
app.set('/assets', express.static(__dirname + '/source/assets/'));

app.use(logger('dev'));
app.use(cookieParser);

app.get('/', function (req, res) {
  res.send('Hello World!');
});

/**
 * 404 Error Handler
 * Creates an error object to be used and passed to pages.
 * TODO move this out of the server.js
 * TODO create generic 500/404 page
 * NOTE this must always be the last route called (i.e. if the server cannot find any other routes this will be called)
 */
 app.use(function(err, req, res, next){
  // error page
  res.status(500).render('error', {error : err});
});

app.use(function(req, res, next) {
  // logic - TODO: Create Error handling here
  // console.log(req);
  res.status(404).render('error', { error: req.originalUrl });
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

module.exports = app;

please help! 请帮忙!

Ok so after a few more hours of debugging the trace led back to some bad NPM package installs which were somehow causing the issue. 好了,经过几个小时的调试之后,该跟踪导致了一些不良的NPM软件包安装,从而以某种方式导致了问题。

I am still not entirely sure what happened, but basically just had to start over from a fresh project and rebuild. 我仍然不完全确定发生了什么,但是基本上只需要从一个新项目重新开始并进行重建。

Thank you for the assistance. 感谢您的帮助。

The following things you have to keep in your mind. 您必须牢记以下几点。

install dependencies: 安装依赖项:

$ cd your_app_name && npm install

Install supervisor and use it: 安装主管并使用它:

npm install supervisor -g

Edit package.json file and replace below lines : 编辑package.json文件并替换以下行:

"scripts": { "start": "supervisor ./bin/www" }, “ scripts”:{“ start”:“ supervisor ./bin/www”},

run the app with debug mode: 以调试模式运行应用程序:

$ DEBUG=your_app_name:* npm start

run app on different port with debug mode: 使用调试模式在其他端口上运行应用程序:

$ PORT=8080 DEBUG=your_app_name:* npm start

check port is already running or not: 检查端口是否已在运行:

$ netstat -anp tcp | grep 3000
$ sudo netstat -lpn |grep :3000

Kill the running port: 终止正在运行的端口:

$ sudo fuser -k 3000/tcp

Convert HTML to Jade: here 将HTML转换为Jade: 此处

http://html2jade.aaron-powell.com/

I hope with above information you can sort out your problem. 希望以上信息可以解决您的问题。

In express the ordering matters You have the hello world route after the error and 404 handler. 在表达订购事项时,您会在错误和404处理程序之后找到一个hello world路线。 You need to reorder them. 您需要重新排序。

// even better create a routes file and include it here
app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.use(function(req, res, next) {
  res.status(404);
  res.send('404: Page not found');
});

app.use(function(err, req, res, next){
  res.status(500);
  res.send('500');
});

Take a look at a express-boilerplate for more details on including routes 看看快速样板 ,了解有关路线的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM