让我们一起学习《Node.js入门》一书吧!
Node.js入门 读完本书之后,你将完成一个完整的web应用,该应用允许用户浏览页面以及上传文件。
里面对一些知识的讲解,让你略窥Node.js的门径。最好一段代码一段代码的写下来,我的习惯是手里拿着笔,边写重点的词,边思索,再敲敲代码。一天到两天你能看完这本书,我看过一遍了,计划再寻摸一遍。有问题大家可以一起探讨。
四个文件在同一个目录下
启动
cmd窗口:
输入 node D:\workspace\nodejs\index.js
也可以定位到当前目录后:
D:\workspace\nodejs\ node index.js
打开
localhost:8080/start
说下,此书最后的例子完整代码中有点问题。你应该会出现 类似no such file or directory ,或者是此文件已经存在的错误提示。修改处已经做了标注:
具体文件:
index.js
var server=require("./server");var router=require("./router");var requestHandlers=require("./requestHandlers");var handle={};handle["/"]=requestHandlers.start;handle["/start"]=requestHandlers.start;handle["/upload"]=requestHandlers.upload;handle["/show"]=requestHandlers.show;server.start(router.route,handle);
server.js
var http = require("http");var url=require("url");function start(route,handle){function onRequest(request,response){var postData="";var pathname=url.parse(request.url).pathname;console.log("Request for"+pathname+" received");route(handle,pathname,response,request);}http.createServer(onRequest).listen(8080);console.log("Server has started");}exports.start=start;
router.js
var http = require("http"); var url=require("url"); function start(route,handle){ function onRequest(request,response){ var postData=""; var pathname=url.parse(request.url).pathname;console.log("Request for"+pathname+" received"); route(handle,pathname,response,request); } http.createServer(onRequest).listen(8080); console.log("Server has started"); } exports.start=start;
requestHandler.js
//var exec=require("child_process").exec;var querystring=require("querystring"),fs=require("fs"),formidable=require("formidable");function start(response){console.log("start was called");var body="<!DOCTYPE html>"+'<head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>'+'这是个测试</title></head><body>'+'<form action="/upload" enctype="multipart/form-data" method="post">'+'<input type="file" name="upload" />'+'<input type="submit" value="Upload file" />'+'</form>'+'</body></html>';response.writeHead(200,{"Content-Type":"text/html"});response.write(body);response.end();}function upload(response,request){console.log("upload was called");var form=new formidable.IncomingForm({uploadDir:"tmp"});//Modify console.log("about to parse");form.parse(request,function(error,fields,files){console.log("parsing done");fs.renameSync(files.upload.path,"tmp/test.jpg");//The Dir,pay attention response.writeHead(200, {"Content-Type": "text/html"});response.write("Received image:<br /><img src='/show' />");response.end();});}function show(response){console.log("Show was callde");fs.readFile("tmp/test.jpg","binary",function(error,file){//The Dir,pay attentionif(error){response.writeHead(500,{"Content-Type":"text/plain"})response.write(error + "\n");response.end();} else {response.writeHead(200, {"Content-Type": "image/jpeg"});response.write(file, "binary");response.end();}})}exports.start=start;exports.upload=upload;exports.show=show;
你可以下载 node.js入门正确示例
转载于:https://www.cnblogs.com/yixiaoheng/p/let-us-learn-nodejs-together-3.html
让我们一起学习《Node.js入门》一书吧!相关推荐
- node.js入门 - 9.api:http
node一个重要任务是用来创建web服务,接下来我们就学习与此相关的一个重要的api -- http.我们使用http.createServer()创建一个http服务的实例,用来处理来自客户的请求. ...
- Node.js 入门到干活,10 个优质项目就够了!
Node.js 在很多大公司都有不错的实践,比如:淘宝.天猫 Web 版,很多页面都是在 Node 服务器上渲染的.还有各种脚手架.前端打包发布工具.构建生态的小工具,也基本都是 Node.js 编写 ...
- 第一天 :学习node.js
第一天 :学习node.js ① node.js环境配置 我学过的语言最简单的一门 直接百度就可以配置 ② 每个入门 的程序都是从helloworld开始 代码如下 : var http=requir ...
- Node.js入门 - 永恒的Hello World!
本文自theprojectspot.com翻译而来,文章原作者为Lee Jacobson, 已经作者授权翻译用于非商业用途.原文地址:猛戳这里进入 介绍 这是我的关于Node.js系列入门教程的第一篇 ...
- Node.js 入门知识点总结
Noed.js 快速入门 前言 node.js 入门 环境配置 hello node.js node.js 获取参数 node.js 模块系统 node.js 路由 node.js 文件操作 使用 n ...
- 【Nodejs】448- 深入学习 Node.js Buffer
预备知识 ArrayBuffer ArrayBuffer 对象用来表示通用的.固定长度的原始二进制数据缓冲区.ArrayBuffer 不能直接操作,而是要通过类型数组对象 或 DataView 对象来 ...
- 【全文】狼叔:如何正确的学习Node.js
说明 2017-12-14 我发了一篇文章<没用过Node.js,就别瞎逼逼>是因为有人在知乎上黑Node.js.那篇文章的反响还是相当不错的,甚至连著名的hax贺老都很认同,下班时读那篇 ...
- html js不触发_「万字整理 」这里有一份Node.js入门指南和实践,请注意查收 ??
前言 什么是 Node.js 呢 ? JS 是脚本语言,脚本语言都需要一个解析器才能运行.对于写在 HTML 页面里的 JS,浏览器充当了解析器的角色.而对于需要独立运行的 JS,NodeJS 就是一 ...
- 小程序 timestamp_通过构建Timestamp微服务应用程序来学习Node.js
小程序 timestamp by Ayo Isaiah 通过Ayo Isaiah 通过构建Timestamp微服务应用程序来学习Node.js (Learn Node.js by building a ...
最新文章
- SQL SERVER 查询所有数据表名称和数据行数
- python isodd()判断奇偶_位运算(1的个数;2.判断奇偶)
- extra加ing_英语词汇学各个章节的内容
- 2020 AI产业图谱启动,勾勒中国AI技术与行业生态
- Windows 2003 Server 设置技巧
- 防火墙(二)SNAT和DNAT
- 【DirectX12】3.配置FBX_SDK
- c语言程序设计对称字符串,C语言程序设计(字符串)
- mysql 字符串搜_mysql – 在表中搜索字符串的SQL查询?
- sublime运行python代码_怎么用sublime运行python
- python zip dict函数
- ubuntu桌面美化mac_Ubuntu16.04 主题美化
- 约束最优化方法 (二) Zoutendijk容许方向法
- 2.CND技术详解---CDN技术概述
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
- 处理键盘上升盖住文本框问题
- java 车牌识别系统_java车牌识别系统 自动识别车牌系统的工作原理
- Ajax传参中文问题
- whisper客服源码_以太坊源码分析—Whisper
- p图软件pⅰc_pic修图软件下载-pic修图 安卓版v16.4.52-PC6安卓网