【nodejs】初识 NodeJS(四)
上节我们把服务器、路由和请求处理程序结合在一起了,下面就编写一个具体的 web 应用。
上传图片的 web 应用
服务器模块(server.js)
var http = require('http');
var url = require('url');
function start(route, handler) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log('Request ' + pathname + ' received.');
route(handler, pathname, response, request);
}
http.createServer(onRequest).listen(8888);
console.log('Server has started.');
}
exports.start = start;
路由模块(route.js)
function route(handler, pathname, response, request) {
console.log('Route a request for ' + pathname);
if (typeof handler[pathname] === 'function') {
handler[pathname](response, request);
} else {
console.log('No request handler found for ' + pathname);
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('404 Not Found');
response.end();
}
}
exports.route = route;
请求处理程序模块(requestHandlers.js)
var querystring = require('querystring'),
fs = require('fs'),
formidable = require('formidable'); // 如果 formidable 模块是全局安装,只能绝对路径调用,如果本地安装,require(formidable)
function start(response) {
console.log('Request handler "start" was called.');
var body = '<html>' +
'<head>' +
'<meta http-equiv="Content-Type" content="text/html; ' +
'charset=UTF-8" />' +
'</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('Request handler "upload" was called.');
var form = new formidable.IncomingForm();
form.uploadDir = 'tmp';
form.parse(request, function(error, fields, files) {
console.log('parsing done');
fs.renameSync(files.upload.path, './tmp/test.png');
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write('Received image:<br>');
response.write('<img src="/show">');
response.end();
});
}
function show(response) {
console.log('Request handler "show" was called.');
fs.readFile('./tmp/test.png', 'binary', function(error, file) {
if (error) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.write(error + '\n');
response.end();
} else {
response.writeHead(200, {
'Content-Type': 'image/png'
});
response.write(file, 'binary');
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
主文件(index.js)
var server = require('./server');
var route = require('./route');
var requestHandlers = require('./requestHandlers');
var handler = {};
handler['/'] = requestHandlers.start;
handler['/start'] = requestHandlers.start;
handler['/upload'] = requestHandlers.upload;
handler['/show'] = requestHandlers.show;
server.start(route.route, handler);
打开终端,启动服务器 node index,浏览器输入 http://localhost:8888/start,上传图片功能就可以使用了。选择一张本地图片,将其上传到服务器,然后浏览器就会显示该图片。
【nodejs】初识 NodeJS(四)的更多相关文章
- 前端笔记之NodeJS(一)初识NodeJS&内置模块&特点
一.NodeJS简介 NodeJS是开发服务器后台的东西,和PHP.JavaEE.python类似,和传统的浏览器的关注DOM的JS完全不同,将JavaScript触角伸到了服务器端.内核是Chrom ...
- nodejs取参四种方法req.body,req.params,req.param,req.body
摘要: nodejs取参四种方法req.body,req.params,req.param,req.body 获取请求很中的参数是每个web后台处理的必经之路,nodejs提供了四种方法来实现. 获取 ...
- 67.nodejs取参四种方法req.body,req.params,req.param,req.body
转自:http://www.cnblogs.com/jkingdom/p/8065202.html 摘要: nodejs取参四种方法req.body,req.params,req.param,req. ...
- 初识NodeJS,一个基于GoogleV8引擎的Javascript运行环境
思考 首先我们来思考一个问题:我们都知道几乎所有现代主流浏览器都全面支持了ECMAScript 5.1版标准,而JavaScript的标准是ECMAScript.那么我们就容易认为JavaScript ...
- java程序员的NodeJS初识篇
摘要 作为一个一直用java来写后端的程序员用NodeJS来写后台,实在不是很爽.这里记下这两个月的NodeJS学习所遇之坑,与java转NodeJS的同仁共勉.学习时间不长,若有理解错误,望指正. ...
- NodeJs学习一NodeJs初识
一.前言 按照惯例,先扯淡,就因为这货,现在才有了各大公司招聘的全栈工程师,正是因为它,让以前只会写前端的人也能写起后端服务器代码来了.所以呢,你招一个会NodeJs的前端,它都能把后端干了,一个人干 ...
- nodejs取参四种方法 req.body, req.params, req.param, req.body
获取请求很中的参数是每个web后台处理的必经之路,nodejs的 express框架 提供了四种方法来实现. req.body req.query req.params req.param() 首先介 ...
- 初识NodeJS
1.JavaScript 模块化规范 浏览器环境 AMD Asynchronous Module Definition RequireJS CMD Common Module Definition S ...
- NodeJS入门(四)—— path对象
很快Node就会迎来4.0的时代,届时将并入现有的iojs,所以先前写过的iojs入门系列直接更名为NodeJS入门. 本篇开始将逐个介绍Node的各主要模块,依循API文档走一遍,但会给出比API文 ...
随机推荐
- BZOJ.5290.[AHOI/HNOI2018]道路(树形DP)
BZOJ LOJ 洛谷 老年退役选手,都写不出普及提高DP= = 在儿子那统计贡献,不是在父亲那统计啊!!!(这样的话不写这个提高DP写记忆化都能过= =) 然后就令\(f[x][a][b]\)表示在 ...
- C++ STL中哈希表Map 与 hash_map 介绍
0 为什么需要hash_map 用过map吧?map提供一个很常用的功能,那就是提供key-value的存储和查找功能.例如,我要记录一个人名和相应的存储,而且随时增加,要快速查找和修改: 岳不群-华 ...
- 05 树莓派安装飞鸽传书 Iptux
2017-08-22 14:47:06 进入页面:https://packages.debian.org/search?keywords=iptux 选择“stretch (stable) ”—— ...
- [POJ2287][Tyvj1048]田忌赛马 (贪心+DP)
瞎扯 很经典的一道题 考前才打 我太菜了QAQ 就是先贪心排序了好 然后在DP 这样比直接DP更容易理解 (其实这题做法还有很多) 代码 #include<cstdio> #include ...
- printf scanf cin cout的区别与特征
printf和scanf是c语言的输入输出,学习c++以后,自然是用cin cout这两个更简单的输入输出 printf scanf 都需要进行格式控制,比较麻烦,但优点是速度比较快,毕竟多做了一些事 ...
- django之模型层(model)--多表相关操作(图书管理小练习)
前面几篇随笔的数据库增删改查操作都是在单表的操作上的,然而现实中不可能都是单表操作,更多的是多表操作,一对一,一对多,多对多的表结构才是我们经常需要处理的,本篇将带我们了解多表操作的一些相关操作.也会 ...
- VS2013中Python学习笔记[基础入门]
前言 在上一节中简单的介绍了在VS2013中如何进行开发Hello World,在VS2013中进行搭建了环境http://www.cnblogs.com/aehyok/p/3986168.html. ...
- 基于SOUI开发一个简单的小工具
基于DriectUI有很多库,比如 Duilib (免费) soui (免费) DuiVision (免费) 炫彩 (界面库免费,UI设计器付费,不提供源码) skinui (免费使用,但不开放源码, ...
- AssemblyInfo.cs文件详解
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq395537505/article/details/49661555 一.前言 .net工程的Pr ...
- Skyline中加载WMTS地图
Skyline中默认是Bing地图,必应虽然免费无偏移,但在国内的影像质量并不是很好.不用担心,Skyline支持多种影像图层,包括WFS.WMS.WMTS地图服务.使用地图作为底图有两个好处: (1 ...