express哪个节点快_用express创建节点js应用程序

express哪个节点快

If you are new to Node.js and want to start creating a simple project in Node.js with expressjs, you are landed on the right place.

如果您不熟悉Node.js,并且想开始使用expressjs在Node.js中创建一个简单项目,那么您来对地方了。

  1. Install Node.js

    安装Node.js

First of all, you need to install Node.js from it’s official website. You can download it according to your platform. Click on the below link to download node.js:

首先,您需要从其官方网站安装Node.js。 您可以根据自己的平台下载。 点击下面的链接下载node.js:

I am using windows so it is showing me windows installers. Download the LTS version and install it with the default options.

我正在使用Windows,因此它向我显示Windows安装程序。 下载LTS版本并使用默认选项进行安装。

To check if Node.js is installed successfully, you can check it in command prompy(in window). If version is shown with the help of below command then Node.js is installed successfully in your machine.

要检查Node.js是否已成功安装,可以在命令提示符(在窗口中)中对其进行检查。 如果在以下命令的帮助下显示了version,则Node.js已成功安装在您的计算机中。

Image for post

2. Create project directory

2.创建项目目录

We need to create a project folder where we can initialize our node js project. You can create a directory with the help of below command and navigate to that directory.

我们需要创建一个项目文件夹,可以在其中初始化节点js项目。 您可以使用以下命令创建目录,然后导航到该目录。

mkdir = This is used to make a directory.

mkdir =用于创建目录。

cd = This is used to change a directory.

cd =用于更改目录。

Image for post

3. Initialize node.js project

3.初始化node.js项目

Before initializing the project, we need to understand a little bit about npm. npm is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. All the packages we will use in our project will be installed from npm.

在初始化项目之前,我们需要对npm有所了解。 npm是JavaScript编程语言的软件包管理器。 它是JavaScript运行时环境Node.js的默认包管理器。 我们将在项目中使用的所有软件包都将从npm安装。

You can use the below command to initialize the project:

您可以使用以下命令来初始化项目:

npm init
Image for post

After typing yes, processing will be done and it will create package.json file inside the project directory. It contains all the information which we have provided at the time of initializing the project.

键入yes后,将完成处理并将在项目目录内创建package.json文件。 它包含我们在初始化项目时提供的所有信息。

4. Create entry file index.js

4.创建条目文件index.js

As we have provided index.js as our entry or main file, we need to create index.js file inside project directory.

由于已将index.js作为条目或主文件提供,因此我们需要在项目目录中创建index.js文件。

Image for post

5. Install express

5.安装快递

As we want to use express with node.js, we need to install express using npm command.

因为我们想将express与node.js一起使用,所以我们需要使用npm命令安装express。

ExpressJS is a web application framework that provides you with a simple API to build websites, web apps and back ends. With ExpressJS, you need not worry about low level protocols, processes, etc.

ExpressJS是一个Web应用程序框架,为您提供了一个简单的API,用于构建网站,Web应用程序和后端。 使用ExpressJS,您无需担心底层协议,流程等。

Use the below command in mydemoapp directory only

仅在mydemoapp目录中使用以下命令

npm install express — save

It will create node_modules folder and package-lock.json file inside mydemoapp project folder.

它将在mydemoapp项目文件夹中创建node_modules文件夹和package-lock.json文件。

Image for post

6. Add code to index.js

6.将代码添加到index.js

Add the following code in index.js file:

在index.js文件中添加以下代码:

var express = require(‘express’);
var app = express();app.get(‘/’, function (req, res)
{
res.send(‘Hey there, we are trying to learn node js with express.’);
});app.get(‘/test’, function (req, res)
{
res.send(‘This is test url.’);
});app.listen(3000, function () {
console.log(‘Example app listening on port 3000!’);
});

Here we are including express module in first line. We are assigning it into a variable which will provide access to predefined functions inside this module. In variable app, we are initializing the module to use the predefined functions in it.

在这里,我们在第一行中包括Express模块。 我们将其分配给一个变量,该变量将提供对该模块内部预定义功能的访问。 在变量app中,我们正在初始化模块以在其中使用预定义的功能。

app.listen() method starts a server and listens on port 3000 for connections. You can use any port here. It responds with “Hey there, we are trying to learn node js with express.” for get requests to the root URL (/). For /test URL, it will respond with “This is test url”. For every other path, it will respond with a Cannot GET.

app.listen()方法启动服务器,并在端口3000上侦听连接。 您可以在此处使用任何端口。 它回应为“嘿,我们正在尝试通过express学习node js。” 获取对根URL(/)的请求。 对于/ test URL,它将以“ This is test url”响应。 对于其他所有路径,它将以Cannot GET响应。

7. Run the app

7.运行应用

Use the below command to run the application

使用以下命令运行应用程序

node index.js
Image for post

It will print the message on the screen which we used in listen method.

它将消息显示在我们在listen方法中使用的屏幕上。

After this, type the localhost with port name in the browser http://localhost:3000/. It will launch the application with default root URL(/).

之后,在浏览器http:// localhost:3000 /中输入端口名称为localhost 。 它将使用默认的根URL(/)启动应用程序。

Image for post

That’s it for today. You have created a node js project with expressjs. You can explore the file structure created in this tutorial.

今天就这样。 您已经使用expressjs创建了一个节点js项目。 您可以探索在本教程中创建的文件结构。

If you like this, please provide your feedback by clicking on like button. Thank you in advance. :)

如果您愿意,请单击“喜欢”按钮提供反馈。 先感谢您。 :)

翻译自: https://medium.com/@vibhuranjan21/create-node-js-application-with-express-3048fb4d8b8b

express哪个节点快

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值