简体   繁体   English

node.js中curl的连接错误

[英]Connection error on curl in node.js

Hi I am currently trying to learn how to create a backend for my app but when trying to create a port 3000 and having curl it produces this error: 嗨,我目前正在尝试学习如何为我的应用程序创建后端,但是当尝试创建端口3000并使其卷曲时,会产生此错误:

NodeTutorial git:(master) ✗ curl -v http://localhost:3000 - the connection is refused. NodeTutorial git:(master)✗curl curl -v http://localhost:3000连接被拒绝。

Please Help. 请帮忙。

My code in my index.js is as follows: 我在index.js代码如下:

var http = require('http'),
express = require('express');

var app = express();
app.set('port', process.env.PORT || 3000);

app.get('/', function (req, res) {
  res.send('<html><body><h1>Hello World</h1></body></html>');
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

Seems your server has not started, try running below steps and see if you are able to connect or not: 似乎您的服务器尚未启动,请尝试按以下步骤运行,看看是否可以连接:

Step 1: Create a directory which will contain all the files related to our app and execute npm init: 步骤1:创建一个目录,其中将包含与我们的应用程序相关的所有文件,并执行npm init:

$ mkdir nodejs-server
$ npm init

Step 2: Install Express as a dependency: 步骤2:安装Express作为依赖项:

$ cd nodejs-server
$ npm install --save express

Step 3: Create default entry point for Node.js ie index.js , inside the same directory we created: 步骤3:在我们创建的同一目录内为Node.js创建默认入口点,即index.js

$ cd nodejs-server
$ touch index.js

Copy the content you have shown in your question in index.js : 将您在问题中显示的内容复制到index.js

var http = require('http'),
express = require('express');

var app = express();
app.set('port', process.env.PORT || 3000);

app.get('/', function (req, res) {
  res.send('<html><body><h1>Hello World</h1></body></html>');
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

Step 4: Move to directory nodejs-server and start the app following below command: 步骤4:移至目录nodejs-server并按照以下命令启动应用程序:

$ cd nodejs-server
$ node index.js

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

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