node 循序渐进
1. 执行
node helloworld.js
2. http 服务器
建 server.js 文件 - node server.js 跑起来 - 浏览器访问 http://localhost:8888/ (服务器控制台观看访问次数)
var http = require("http");
var count=(function(){
var num=0;
return function(){
num++;
return num;
}
}());
http.createServer(function (request, response) {
response.writeHead(200, {
"Content-Type": "text/plain"
});
response.write("Hello World");
response.end();
console.log('server'+count());
}).listen(8888);
3. http 服务器 模块化方式实现
建主文件 入口 index.js - 写模块化功能并导出对应接口 server.js - 运行 node index - 访问 http://localhost:8888/
index.js
function handle(request, response) {
response.writeHead(200, {
"Content-Type": "text/plain"
});
response.write("Hello World");
response.end();
console.log('server' + count());
}
var count = (function () {
var num = 0;
return function () {
num++;
return num;
}
}());
var http = require("http");
function start(){
http.createServer(handle).listen(8888);
console.log("Server working.");
}
exports.start = start;
4. http 服务器 + router 模块化 整合
index.js
var count = (function () {
var num = 0;
return function () {
num++;
return num;
}
}());
var http = require("http");
var url = require("url");
function server(route){
function handle(request, response) {
var pathname = url.parse(request.url).pathname;
route(pathname);
response.writeHead(200, {
"Content-Type": "text/plain"
});
response.write("Hello World");
response.end();
console.log('server' + count());
}
http.createServer(handle).listen(8888);
console.log("Server working.");
}
exports.server = server;
5. 低耦合 http 服务器
var http = require("http");
var url = require("url");
function server(route, handle){
function requestHandle(request, response) {
var pathname = url.parse(request.url).pathname;
route(handle, pathname);
response.writeHead(200, {
"Content-Type": "text/plain"
});
response.write("Hello World");
response.end();
}
http.createServer(requestHandle).listen(8888);
console.log("Server working.");
}
exports.server = server;
router.js
function start() {
console.log("Request handler 'start' was called.");
}
function upload() {
console.log("Request handler 'upload' was called.");
}
exports.start = start;
exports.upload = upload;
改变为
node 循序渐进的更多相关文章
- babeljs源码
babel.min.js!function(e,t){"object"==typeof exports&&"object"==typeof mo ...
- Node.js 教程 03 - 创建HTTP服务器
前言: 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请 ...
- Node入门(转)
原文链接:http://www.nodebeginner.org/index-zh-cn.html Node入门 作者: Manuel Kiessling翻译: goddyzhao & Gra ...
- Node初学者入门,一本全面的NodeJS教程(转载)
分类 JS学习 发布 ourjs 2013-12-02 注意 转载须保留原文链接,译文链接,作者译者等信息. 作者: Manuel Kiessling 翻译: goddyzhao &a ...
- Node.js 项目搭建
关于 本书致力于教会你如何用Node.js来开发应用,过程中会传授你所有所需的“高级”JavaScript知识.本书绝不是一本“Hello World”的教程. 状态 你正在阅读的已经是本书的最终版. ...
- 初学node.js有感二
node.js进阶 一.回顾与继续 对于一种语言的认识都是经历这样的一个过程的,首先从原生的环境(CMD)中开始学习,找到一门语言之间各种引用的本质和相互之间的调用方式,明澈各种依赖关系,在这个基 ...
- 使用Sublime Text 或 vs2017开发Node.js程序
在学习一门开发语言时,为了从简单的方式入手,有时候直接用Notepad开始敲代码.曾经我也这样干过,这样做简洁而不简单啊! 随着时间的流逝,人也变得懒惰起来,做事前总是想借助一些工具来搞事情.< ...
- Node.js学习看这里:基础、进阶、文章
Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google Chrome V8引擎进行了封装,它主要用于创建快速的.可扩展的网络应用. Node.js采用事件 ...
- Node.js链式回调
由于异步的关系,代码的书写顺序可能和执行顺序并不一样,可能想先执行A再执行B,但由于异步可能B要先于A执行.例如在OC中使用AFnetworking请求数据然后刷新页面,由于网络请求是用block实现 ...
随机推荐
- iOS 开发笔记-Storyboard
什么时候用Pust,什么时候用Modal? 一般情况下,是导航控制器点过去的,都使用Pust.如果是相对独立的,则用Modal,比如是导航上面的+添加之类. 关闭一个Modal -(IBAction) ...
- 文件操作(CRT、C++、WIN API、MFC)
一.使用CRT函数文件操作 二.使用标准C++库 std::fstream std::string 1)std::string对象内部存储了一个C的字符串,以'\0'结尾的. 2)std::strin ...
- 搭建Python3的jupyter notebook服务器
摘要:搭建Python3 jupyter notebook. 激活Python3后,进入Python交互环境 1. 登陆远程服务器 2. 生成配置文件 1. $jupyter notebook --g ...
- python3.4学习笔记(九) Python GUI桌面应用开发工具选择
python3.4学习笔记(九) Python GUI桌面应用开发工具选择 Python GUI开发工具选择 - WEB开发者http://www.admin10000.com/document/96 ...
- new Runnable中的 this
package com.test; public class BB extends AA{ public String bb = "bbbbb"; public void gg() ...
- Linux中Postfix邮件安装Maildrop(八)
Postfix使用maildrop投递邮件 Maildrop是本地邮件投递代理(MDA), 支持过滤(/etc/maildroprc).投递和磁盘限额(Quota)功能. Maildrop是一个使用C ...
- Python入门之面向对象的__init__和__new__方法
Python入门之面向对象的__init__和__new__方法
- python3 isinstance()判断元素是否是字符串、int型、float型
python3 isinstance()判断元素是否是字符串.int型.float型 isinstance是Python中的一个内建函数 语法: isinstance(object, classinf ...
- 02: tornado进阶篇
目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 自定制t ...
- 逆向与BOF基础——注入shellcode并执行&Return-to-libc
逆向与BOF基础--注入shellcode并执行 准备阶段 下载安装execstack. 本次实验实验的shellcode是心远的文章中生成的代码,即\x31\xc0\x50\x68\x2f\x2f\ ...