js 子节点父节点兄弟节点_节点js与节点js和express的世界

js 子节点父节点兄弟节点

什么是Node.js?(What is Node.js?)

Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser. Node JS enables us to write server-side Javascript code. We know that javascript runs only on the browser. But still, it works on the server too because it is built on Chrome’s V8 JavaScript engine. With Node JS we can build different types of applications like web servers, command Line Applications, Rest APIs, and so on. Node JS is popular for building web servers. Node JS provides us asynchronous and cross-platform run time environment. Node JS follows non-blocking execution which makes it the fastest tool for build servers and applications.

Node.js是一个开放源代码,跨平台JavaScript运行时环境,可在Web浏览器之外执行JavaScript代码。 Node JS使我们能够编写服务器端Javascript代码。 我们知道javascript仅在浏览器上运行。 但是它仍然可以在服务器上运行,因为它是基于Chrome的V8 JavaScript引擎构建的。 使用Node JS,我们可以构建不同类型的应用程序,例如Web服务器,命令行应用程序,Rest API等。 Node JS在构建Web服务器方面很流行。 Node JS为我们提供了异步和跨平台的运行时环境。 Node JS遵循无阻塞执行,这使其成为构建服务器和应用程序的最快工具。

1.安装节点 (1. Installing Node)

To create a node project, you should have a node installed in your local machine. To do that, just head towards https://nodejs.org/en/ and install the node.

要创建节点项目,您应该在本地计算机上安装一个节点。 为此,只需前往https://nodejs.org/en/并安装节点。

2.创建一个节点项目 (2. Creating a Node Project)

Create a new directory and initialize node with the command npm init. Npm is a package manager where all the javascript packages reside. We download all the javascript packages through npm.

创建一个新目录并使用命令npm init初始化节点。 Npm是所有Javascript软件包所在的软件包管理器。 我们通过npm下载所有的javascript软件包。

mkdir helloworld
cd helloworld/
npm init -y

Here I have added an extra tag -y with the command npm init. Usually, when we type npm init, It asks as certain questions. To skip those, I am just adding this -y tag to the command.

在这里,我使用命令npm init添加了一个额外的tag -y 。 通常,当我们输入npm init ,它会询问某些问题。 要跳过这些,我只是将此-y标记添加到命令中。

After executing the command, a package.json file generated in the project root directory. This holds all the metadata relevant to the project.

执行命令后,在项目根目录中生成一个package.json文件。 这将保存与项目相关的所有元数据。

On this file, we can see something called scripts. This is the place where we add our own commands for the project. I am creating a new script command which starts my server when I type npm start. The script tells node that it should run the command node index.js every time when I execute the command npm start.

在此文件上,我们可以看到称为脚本的内容。 在这里,我们为项目添加了自己的命令。 我正在创建一个新的脚本命令,当我键入npm start时,它将启动服务器。 该脚本告诉节点,每当我执行命令npm start时,它应该运行命令node index.js

package.json

package.json

"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},

3.配置Express (3. Configure Express)

Now let’s create our server. Here we are creating our server using Express.js. Express JS is an open-source web framework for node JS. It is designing for building web apps and APIs. The below command installs express to our project.

现在,让我们创建服务器。 在这里,我们使用Express.js创建服务器。 Express JS是用于节点JS的开源Web框架。 它是为构建Web应用程序和API而设计的。 下面的命令将express安装到我们的项目中。

npm install express --save

We are using express to create a new server that will be running on the port 8000. Also for the demonstration, I am creating a route that returns hello world.

我们正在使用express创建一个新的服务器,该服务器将在8000端口上运行。同样,为了进行演示,我正在创建一个返回hello world的路由。

index.js

index.js

var express = require('express');
var app = express();app.get('/', function (req, res) {
res.send('Hello World!');
});app.listen(8000, function () {
console.log('Listening to Port 8000');
});

Now start the server with the command npm start. Open your browser and navigate to http://localhost:8000/. You should see Hello world getting displayed on the main page.

现在,使用命令npm start服务器。 打开浏览器并导航到http:// localhost:8000 / 。 您应该看到Hello world在主页上显示。

npm start
Image for post
OutPut for the above example
以上示例的OutPut

Feel free to contact me for any queries. Email: sjlouji10@gmail.com. Linkedin: https://www.linkedin.com/in/sjlouji/

如有任何疑问,请随时与我联系。 电子邮件:sjlouji10@gmail.com。 Linkedin: https : //www.linkedin.com/in/sjlouji/

Github: https://github.com/sjlouji/

GitHub: https : //github.com/sjlouji/

Happy coding!

编码愉快!

翻译自: https://medium.com/javascript-in-plain-english/node-js-hello-world-with-node-js-and-express-61dd390c0b14

js 子节点父节点兄弟节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值