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实现 ...
随机推荐
- BCB 按钮添加背景图
使用控件:TBitBtn 位于 Additional分类 属性:GlyPh
- Javascript-涨工资案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Hibernate—部分
数据持久化的3种方式: merge()方法: 先得到对象的副本:再判断, 如果副本为瞬时状态,则用save()插入 如果副本为游离状态,则用update()更新 最终都是不改变传入对象的状态 save ...
- 2:3 Action的配置
< 一 作用> 一:封装工作单元(相当于是控制层,封装出modelAndView) 二:定义name属性接受前台传过来的数据,再定义message属性,用于存放返回前台页面展示的数 据,实 ...
- Codeforces Round #246 (Div. 2) D E
这题说的是给了一个字符串当前缀和后缀相同的时候就计算此时的 整个串种拥有这样的子串友多少个,没想到用KMP解 用0开头的那种类型的 KMP 今天刚好也学了一下,因为KMP的作用是找出最长前缀 KMP ...
- Python:slice与indices
slice: eg: >>>e=[0,1,2,3,4,5,6] >>>s=slice(2,3) >>>e[s] [2] slice的区间左闭右开[ ...
- linux chkconfig 管理服务开机自启动
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...
- 计算概论(A)/基础编程练习1(8题)/6:判断闰年
#include<stdio.h> int isLeap(int year) { // 必须先判断是平年的情况 后判断闰年的情况 == && year%!=) || yea ...
- SQL学习之SQL注入总结
Sql注入定义: 就是通过把sql命令插入到web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行的sql命令的目的. sql注入分类: 基于联合查询 基于错误回显 基于盲注,分时间盲 ...
- 03: KindEditor (HTML可视化编辑器)
目录: 1.1 kindEditor常用配置参数 1.2 kindEditor下载与文件说明 1.3 kindEditor实现上传图片.文件.及文件空间管理 1.1 kindEditor常用配置参数返 ...