服務(wù)器端的JavaScript腳本 Node.js 使用入門
更新時(shí)間:2012年03月07日 08:24:09 作者:
觸爪伸向傳說(shuō)中的Server-Side Javascrpt。后端JS最出名無(wú)疑是Ryan Dahl的node.js,另一個(gè)是aptana IDE提供商搞出的jaxer,這里討論node.js的使用
首先下載node.js,然后解壓到E盤,改名為node,然后開(kāi)始菜單輸入cmd,用cd命令切換到nodejs的解壓目錄:

第一個(gè)例子:hello world。
在node目錄下建立hello.js文件,然后在里面輸入:
var sys = require("sys");
sys.puts("Hello world");
然后我們?cè)诿_(tái)中輸入命令node hello.js,就能看到命名臺(tái)輸出結(jié)果Hello world。
第二個(gè)例子:hello world2。
好了,這次我們?cè)噺挠斡[器中輸出hello world。在node目錄下建立http.js,然后輸入:
var sys = require("sys"),
http = require("http");
http.createServer(function(request, response) {
response.sendHeader(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.close();
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
然后我們?cè)诿_(tái)中輸入命令node http.js,在瀏覽器輸入http://localhost:8080/


第三個(gè)例子:hello world2。
node.js提供一個(gè)Buffer類用于轉(zhuǎn)換不同編碼的字符串。目前支持三種類型:'ascii','utf8'與'binary'。詳見(jiàn)這里
var Buffer = require('buffer').Buffer,
buf = new Buffer(256),
len = buf.write('\u00bd + \u00bc = \u00be', 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));
第四個(gè)例子:hello world3。
//synopsis.js
//synopsis 摘要, 梗概,大綱
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
前臺(tái)地址欄:http://localhost:8124/
第五個(gè)例子:編譯C文件
#include #include int main(){ printf("Hello World!!!"); exit(0); }

第一個(gè)例子:hello world。
在node目錄下建立hello.js文件,然后在里面輸入:
復(fù)制代碼 代碼如下:
var sys = require("sys");
sys.puts("Hello world");
然后我們?cè)诿_(tái)中輸入命令node hello.js,就能看到命名臺(tái)輸出結(jié)果Hello world。
第二個(gè)例子:hello world2。
好了,這次我們?cè)噺挠斡[器中輸出hello world。在node目錄下建立http.js,然后輸入:
復(fù)制代碼 代碼如下:
var sys = require("sys"),
http = require("http");
http.createServer(function(request, response) {
response.sendHeader(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.close();
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
然后我們?cè)诿_(tái)中輸入命令node http.js,在瀏覽器輸入http://localhost:8080/


node.js提供一個(gè)Buffer類用于轉(zhuǎn)換不同編碼的字符串。目前支持三種類型:'ascii','utf8'與'binary'。詳見(jiàn)這里
復(fù)制代碼 代碼如下:
var Buffer = require('buffer').Buffer,
buf = new Buffer(256),
len = buf.write('\u00bd + \u00bc = \u00be', 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));
第四個(gè)例子:hello world3。
復(fù)制代碼 代碼如下:
//synopsis.js
//synopsis 摘要, 梗概,大綱
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
前臺(tái)地址欄:http://localhost:8124/
第五個(gè)例子:編譯C文件
復(fù)制代碼 代碼如下:
#include #include int main(){ printf("Hello World!!!"); exit(0); }
您可能感興趣的文章:
- Node.js實(shí)戰(zhàn) 建立簡(jiǎn)單的Web服務(wù)器
- node.js+Ajax實(shí)現(xiàn)獲取HTTP服務(wù)器返回?cái)?shù)據(jù)
- Node.js:Windows7下搭建的Node.js服務(wù)(來(lái)玩玩服務(wù)器端的javascript吧,這可不是前端js插件)
- 教你如何使用node.js制作代理服務(wù)器
- [將免費(fèi)進(jìn)行到底]在Amazon的一年免費(fèi)服務(wù)器上安裝Node.JS, NPM和OurJS博客
- 使用Node.js實(shí)現(xiàn)一個(gè)簡(jiǎn)單的FastCGI服務(wù)器實(shí)例
- 為Node.js程序配置使用Nginx服務(wù)器的簡(jiǎn)明教程
- Node.js實(shí)現(xiàn)簡(jiǎn)單聊天服務(wù)器
- Node.js 服務(wù)器端應(yīng)用開(kāi)發(fā)框架 -- Hapi.js
- 利用node.js本地搭建HTTP服務(wù)器
相關(guān)文章
總結(jié)28個(gè)令人驚艷的JavaScript單行代碼
JavaScript作為一種強(qiáng)大而靈活的腳本語(yǔ)言,充滿了許多令人驚艷的特性,本文將帶你探索28個(gè)令人驚艷的JavaScript單行代碼,展示它們的神奇魅力,感興趣的同學(xué)跟著小編一起來(lái)看看吧2023-12-12
學(xué)習(xí)JavaScript設(shè)計(jì)模式之迭代器模式
這篇文章主要為大家介紹了JavaScript設(shè)計(jì)模式中的迭代器模式,對(duì)JavaScript設(shè)計(jì)模式感興趣的小伙伴們可以參考一下2016-01-01
詳解TS對(duì)象擴(kuò)展運(yùn)算符和rest運(yùn)算符
這篇文章主要介紹了詳解TS對(duì)象擴(kuò)展運(yùn)算符和rest運(yùn)算符,對(duì)TypeScript感興趣的同學(xué),可以參考下2021-05-05
微信小程序?qū)崿F(xiàn)根據(jù)日期和時(shí)間排序功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)根據(jù)日期和時(shí)間排序功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
js?select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼
這篇文章主要介紹了js?select支持手動(dòng)輸入實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-05-05

