Introduction to Node.js(2)

本文介绍了如何识别Node.js文件,演示了通过index.js调用server.js启动一个简单的HTTP服务器,并讲解了如何安装和使用外部模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction to Node.js(2)

1 How to recognize your Node.js files?

Node-js
|--- index.js
|--- server.js

index.js:

var server = require("./server.js");
server.start();

server.js:

var http = require("http");
function start() {
    http.createServer(function(request, response) {
        console.log("Receive a request.");
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }).listen(8765, "127.0.0.1");
}
exports.start = start;

2 How to install an external module?

It’s very very easy. The only requirement is that your computer has been connected to Internet. The method to install Node.js modules is just like the way to install softwares on Linux using apt-get, or on Mac OS X using marport.

npm install SOME_MODULE

What does npm stand for?

Perhaps it is NodeJS Pakage Management.

3 How to use a module?

I guess you’ve already known that.

var yourModule = require("./someModule.js");

or

var module = require("module");

-

转载请注明来自柳大的优快云博客:blog.youkuaiyun.com/poechant

-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值