创建一个名为hello.js的文件:
/**
* comment.
*/
console.log("Hello World!");
现在,我们可以从命令行执行这个文件
node hello.js
Hello World!
变量
变量在JavaScript中使用var关键字定义。例如,以下代码段创建一个变量foo并将其记录到控制台。
var myData = 123;
console.log(myData);
上面的代码生成以下结果。
JavaScript运行时有机会定义一些我们可以在代码中使用的全局变量。其中一个是控制台对象。控制台对象包含一个成员函数(log),它接受任意数量的参数并将它们打印到控制台。
第一个Web服务器
var http = require("http");
/*from www.w3cschool.cn*/
function process_request(req, res) {
var body = 'Thanks for calling!\n';
var content_length = body.length ;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
res.end(body);
}
var s = http.createServer(process_request);
s.listen(8080);
要运行它,只需键入
node web.js
或使用:
curl -i http://localhost:8080
HTTP/1.1 200 OK Content-Length: 20 Content-Type: text/plain Date: Tue, 15 Feb 2013 03:05:08 GMT Connection: keep-alive Thanks for calling!
curl
要启动它,只需在命令提示符或PowerShell中键入:
C:\Users\abc\curl --help
Wget
C:\Users\abc\wget --help
注意
node debug web.js
转载请注明:www.ainoob.cn » Node.js 拓展教程