当前位置: 首页 > 工具软件 > Cool-Node > 使用案例 >

《The Node Beginner Book》- Node.js环境和浏览器环境下的JS

端木权
2023-12-01

JavaScript and You

Before we talk about all the technical stuff, let's take a moment and talk about you and your relationship with JavaScript. This chapter is here to allow you to estimate if reading this document any further makes sense for you.
在聊技术干货之前,让我们先讲一讲你和JavaScriptd的关系。本章的主要目的是想让你估计,对你而言是否有必要继续阅读后续章节的内容。

If you are like me, you started with HTML "development" long ago, by writing HTML documents. You came across this funny thing called JavaScript, but you only used it in a very basic way, adding interactivity to your web pages every now and then.
如何你和我一样,你在很久远之前就开始使用HTML进行“开发”,在写HTML文档中,你遇到了一个叫做JavaScript的有趣的东西,但是你仅仅对JavaScript有一个很基础的使用方法—为你的网页增添交互行为。

What you really wanted was "the real thing", you wanted to know how to build complex web sites - you learned a programming language like PHP, Ruby, Java, and started writing "backend" code.
那什么是你真正想要的“干货 ”,你想知道如何构建复杂的web站点—于是你学习了例如PHP,Ruby,Java这样的编程语言,开始写“后端”代码。

Nevertheless, you kept an eye on JavaScript, you saw that with the introduction of jQuery, Prototype and the likes, things got more advanced in JavaScript land, and that this language really was about more than window.open().
然而,你仍然关注着JavaScript,随着jQuery,Prototype这些技术的介绍,你慢慢了解到JavaScript的进阶技能,也真正认识到这门语言不再是简单的window.open()

However, this was all still frontend stuff, and although it was nice to have jQuery at your disposal whenever you felt like spicing up a web page, at the end of the day you were, at best, a JavaScript user, but not a JavaScript developer.
但是,这些都是前端技术,尽管在使用jQuery处理增强你的web页面的效果时会感觉很棒,但是到最后,你最多只是一个JavaScript用户,而不是一个JavaScript开发者。

And then came Node.js. JavaScript on the server, how cool is that?
Node js的到来,带来了服务端的JavaScript,这多酷啊。

You decided that it's about time to check out the old, new JavaScript. But wait, writing Node.js applications is one thing; understanding why they need to be written the way they are written means - understanding JavaScript. And this time for real.
你决定是时候重拾这又熟悉又陌生的JavaScript了。但是等一等,写Node.js应用是一件事,而理解为何需要按照他们那种方式书写就是另外一件事了,这意味着你要理解JavaScript。这次是玩真的了。

Here is the problem: Because JavaScript really lives two, maybe even three lives (the funny little DHTML helper from the mid-90's, the more serious frontend stuff like jQuery and the likes, and now server-side), it's not that easy to find information that helps you to learn JavaScript the "right" way, in order to write Node.js applications in a fashion that makes you feel you are not just using JavaScript, you are actually developing it.
那么这就有一个问题:因为JavaScript真正意义上存在两种,或许是三种形态(在中世纪90年代JavaScript作为增强DHTML的小玩意,到像jQuery这样严格意义上的前端技术时期,一直到现在的服务端技术),因此,很难找到一个“正确”的方式来学习JavaScript,为了编写Node.js应用勉强让你觉得不再是使用JavaScript,而是真正的开发JavaScript。(真的超不通顺)

Because that's the catch: you already are an experienced developer, you don't want to learn a new technique by just hacking around and mis-using it; you want to be sure that you are approaching it from the right angle.
因为这就是关键:你已经是一个有经验的开发者,你不再想通过到处寻找各种乱七八糟的方法来学习一个新技术,而是想要确保自己是通过一个正确的方式来学习这个新技术。

There is, of course, excellent documentation out there. But documentation alone sometimes isn't enough. What is needed is guidance.
当然了,市面上还存在着很多优秀的学习JavaScript的文档。但是单靠这些文章是不够的,您需要的是指导。

My goal is to provide a guide for you.
我的目标就是给你提供一份指导。

A word of warning

There are some really excellent JavaScript people out there. I'm not one of them.
有许多优秀的JavaScript程序员,我并不是其中一员。

I'm really just the guy I talked about in the previous paragraph. I know a thing or two about developing backend web applications, but I'm still new to "real" JavaScript and still new to Node.js. I learned some of the more advanced aspects of JavaScript just recently. I'm not experienced.
我就是以上篇幅描述的那个我。我熟悉如何开发后端web应用,但是我仍然是一个“真正”的JavaScript和Node.js的新手。最近我也在学习一些JavaScript的高级概念,还没有实践经验。

Which is why this is no "from novice to expert" book. It's more like "from novice to advanced novice".
所以呢,这本书不是一本“从入门到精通”的书,它更像是一个“从初级入门到高级入门”的书。

If I don't fail, then this will be the kind of document I wish I had when starting with Node.js.
如果成功的话,这本书将会是我当初开始学习Node.js时候最希望拥有的书籍。

Server-side JavaScript

The first incarnations of JavaScript lived in browsers. But this is just the context. It defines what you can do with the language, but it doesn't say much about what the language itself can do. JavaScript is a "complete" language: you can use it in many contexts and achieve everything with it you can achieve with any other "complete" language.
JavaScript最早运行在浏览器中,但浏览器仅仅是一个上下文,它定义了可以使用JavaScript做什么,但并没有阐明太多JavaScript这门语言本身可以做什么。JavaScript是一门“完整”的语言:它可以使用在不同的上下文中,其能力与其他同类语言相比有过之而无不及。

Node.js really is just another context: it allows you to run JavaScript code in the backend, outside a browser.
实际上Node.js就是另一种上下文,它允许脱离浏览器在后端运行JavaScript代码。

In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does, by making use of Google's V8 VM, the same runtime environment for JavaScript that Google Chrome uses.
为了实现JavaScript在后端运行,代码需要先被解释再正确的执行。Node.js的原理就是如此,它通过使用Google的V8虚拟机,来解释和执行JavaScript代码。

Plus, Node.js ships with a lot of useful modules, so you don't have to write everything from scratch, like for example something that outputs a string on the console.
除此之外,伴随着Node.js的还有许多有用的模块,它们可以简化很多重复的劳作,比如向终端输出字符串。

Thus, Node.js is really two things: a runtime environment and a library.
因此,Node.js事实上既是一个运行时环境,同时又是一个库。

In order to make use of these, you need to install Node.js. Instead of repeating the process here, I kindly ask you to visit the official installation page. Please come back once you are up and running.
要使用Node.js,首先需要进行安装。关于如何安装Node.js,这里就不赘述了,可以直接参考官方的安装指南。安装完成后,继续回来阅读本书下面的内容。

"Hello World"

Ok, let's just jump in the cold water and write our first Node.js application: "Hello World".
好了,让我们一起跳进冰冷的水中(what?),开始我们第一个Node.js应用 "Hello World"。

Open your favorite editor and create a file called helloworld.js. We want it to write "Hello World" to STDOUT, and here is the code needed to do that:
打开你最爱的编译器,并创建一个文件取名为helloworld.js。我们要做就是向STDOUT输出“Hello World”,如下是实现该功能的代码:

console.log("Hello World");

Save the file, and execute it through Node.js:
保存文件,通过Node.js来执行:(命令行中写入)

node helloworld.js

This should output Hello World on your terminal.
正常的话,就会在终端输出Hello World 。
Ok, this stuff is boring, right? Let's write some real stuff.
好吧,我承认这个应用是有点无趣,那么下面我们就来写点“干货”。

 类似资料: