Home > Web Front-end > JS Tutorial > body text

nodejs入门详解(多篇文章结合)_javascript技巧

WBOY
Release: 2016-05-16 17:55:42
Original
899 people have browsed it

Node.js是一套用来编写高性能网络服务器的JavaScript工具包,一系列的变化由此开始。比较独特的是,Node.js会假设你是在POSIX环境下运行它Linux 或 Mac OS X。如果你是在Windows下,那就需要安装MinGW以获得一个仿POSIX的环境。在Node中,Http是首要的。Node为创建http服务器作了优化,所以你在网上看到的大部分示例和库都是集中在web上(http框架、模板库等)。

首先,去http://nodejs.org 下载安装。我下的版本是0.6.6。安装很简单,下一步下一步就哦了。
我的安装目录是C:\Program Files (x86)\nodejs。

一、helloworld
在nodejs安装目录中新建一个文件hello.js,里面敲一行代码

复制代码 代码如下:

console.log('hello, nodejs.') ;

进入命令行控制台,进入到nodejs目录敲node hello.js

控制台输出了“hello, nodejs.”
二、web版的helloworld
在nodejs安装目录中新建一个http.js,代码如下

复制代码 代码如下:

var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.end();
}).listen(8000);

在命令行中启动服务,敲 node  http.js

然后打开浏览器地址栏输入http://localhost:8000/,看见页面上输出Hello World! 就成功了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!