node创建服务器
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8888);
console.log('Server running at http://127.0.0.1:8888/');
node文件
我是fs 用noded调用
var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
console.log("程序执行结束!");
Node模块系统
简单模式
var hello = require('./hello');
hello.world();
exports.world = function() {
console.log('Hello World');
}
深入模式
function Hello() {
var name;
this.setName = function(thyName) {
name = thyName;
};
this.sayHello = function() {
console.log('Hello ' + name);
};
};
module.exports = Hello;
var Hello = require('./hello');
hello = new Hello();
hello.setName('BYVoid');
hello.sayHello();
Node路由
var http = require("http");
var url = require("url");
function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
var server = require("./server");
var router = require("./router");
server.start(router.route);