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

第一個例子:hello world。
在node目錄下建立hello.js文件,然后在里面輸入:
var sys = require("sys");
sys.puts("Hello world");
然后我們在命名臺中輸入命令node hello.js,就能看到命名臺輸出結(jié)果Hello world。
第二個例子:hello world2。
好了,這次我們試從游覽器中輸出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/");
然后我們在命名臺中輸入命令node http.js,在瀏覽器輸入http://localhost:8080/


第三個例子:hello world2。
node.js提供一個Buffer類用于轉(zhuǎn)換不同編碼的字符串。目前支持三種類型:'ascii','utf8'與'binary'。詳見這里
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));
第四個例子: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/');
前臺地址欄:http://localhost:8124/
第五個例子:編譯C文件
#include #include int main(){ printf("Hello World!!!"); exit(0); }

第一個例子:hello world。
在node目錄下建立hello.js文件,然后在里面輸入:
復(fù)制代碼 代碼如下:
var sys = require("sys");
sys.puts("Hello world");
然后我們在命名臺中輸入命令node hello.js,就能看到命名臺輸出結(jié)果Hello world。
第二個例子:hello world2。
好了,這次我們試從游覽器中輸出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/");
然后我們在命名臺中輸入命令node http.js,在瀏覽器輸入http://localhost:8080/


node.js提供一個Buffer類用于轉(zhuǎn)換不同編碼的字符串。目前支持三種類型:'ascii','utf8'與'binary'。詳見這里
復(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));
第四個例子: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/');
前臺地址欄:http://localhost:8124/
第五個例子:編譯C文件
復(fù)制代碼 代碼如下:
#include #include int main(){ printf("Hello World!!!"); exit(0); }
您可能感興趣的文章:
- Node.js實(shí)戰(zhàn) 建立簡單的Web服務(wù)器
- node.js+Ajax實(shí)現(xiàn)獲取HTTP服務(wù)器返回數(shù)據(jù)
- Node.js:Windows7下搭建的Node.js服務(wù)(來玩玩服務(wù)器端的javascript吧,這可不是前端js插件)
- 教你如何使用node.js制作代理服務(wù)器
- [將免費(fèi)進(jìn)行到底]在Amazon的一年免費(fèi)服務(wù)器上安裝Node.JS, NPM和OurJS博客
- 使用Node.js實(shí)現(xiàn)一個簡單的FastCGI服務(wù)器實(shí)例
- 為Node.js程序配置使用Nginx服務(wù)器的簡明教程
- Node.js實(shí)現(xiàn)簡單聊天服務(wù)器
- Node.js 服務(wù)器端應(yīng)用開發(fā)框架 -- Hapi.js
- 利用node.js本地搭建HTTP服務(wù)器
相關(guān)文章
學(xué)習(xí)JavaScript設(shè)計模式之迭代器模式
這篇文章主要為大家介紹了JavaScript設(shè)計模式中的迭代器模式,對JavaScript設(shè)計模式感興趣的小伙伴們可以參考一下2016-01-01
詳解TS對象擴(kuò)展運(yùn)算符和rest運(yùn)算符
這篇文章主要介紹了詳解TS對象擴(kuò)展運(yùn)算符和rest運(yùn)算符,對TypeScript感興趣的同學(xué),可以參考下2021-05-05
微信小程序?qū)崿F(xiàn)根據(jù)日期和時間排序功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)根據(jù)日期和時間排序功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
js?select支持手動輸入功能實(shí)現(xiàn)代碼
這篇文章主要介紹了js?select支持手動輸入實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-05-05

