首页 > 其他 > 详细

node入门开发遇到的问题

时间:2015-04-20 16:17:35      阅读:98      评论:0      收藏:0      [点我收藏+]

最近在看node入门这本书,https://cnodejs.org/getstart

里面是跟随作者完成一个小的demo,书中不免会有遗漏的,下面是我在实现里面最后一个例子时遇到的问题,希望能够帮助其他人。

先来看看整体的代码:

index.js

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-4-18
 * Time: 下午6:08
 * To change this template use File | Settings | File Templates.
 */
var server = require("./server.js");
var router = require("./router.js");
var requestHandlers = require("./requestHandlers");

var handle ={};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
handle["/show"] = requestHandlers.show;

server.start(router,handle);

server.js

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-4-18
 * Time: 下午5:08
 * To change this template use File | Settings | File Templates.
 */
var http = require("http");
var url = require("url");

function start(route,handle){
    function onRequest(request,response){
        var postData = "";
        var path = url.parse(request.url).pathname;
        console.log("Request for"+path+" received");
        route.route(handle,path,response,request);
        /*request.addListener("data",function(postDataChunk){
           postData += postDataChunk;
            console.log("Receive post data chunk ‘"+postDataChunk+"‘.");
        });

        request.addListener("end",function(){
            route.route(handle,path,response,postData);
        });*/
    }
    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
}

exports.start = start;

router.js

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-4-18
 * Time: 下午6:49
 * To change this template use File | Settings | File Templates.
 */
 function route(handle,pathname,response,request){

     console.log("About to route a request for"+pathname);
    if(typeof handle[pathname] === ‘function‘)
    {
        handle[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

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-4-18
 * Time: 下午8:38
 * To change this template use File | Settings | File Templates.
 */
var exec = require("child_process").exec();
var querystring = require("querystring"),
    fs = require("fs"),
    util = require(‘util‘),
    formidable = require("formidable");

function start(response,request)
{
    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();
    console.log("about to parse");
    form.parse(request, function(error, fields, files) {
        console.log("parsing done");
        var readStream = fs.createReadStream(files.upload.path);
        var writeStream = fs.createWriteStream("/tmp/test.png");

        util.pump(readStream, writeStream, function() {
            fs.unlinkSync(files.upload.path);
        });
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write("received image:<br/>");
        response.write("<img src=‘/show‘/>");
        response.end();
    });
}

function show(response,request)
{
   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;

接下来来看一下遇到的问题:

1、在使用指令

npm install formidable

你会发现在运行后,控制台会输出

技术分享

但是在npm(包管理器)并没有找到这个module,运行代码时报错Error: Cannot find module ‘formidable‘,这就是包没有引用到。

解决办法是:

打开cmd,使用cd命令切换到你当前项目所在目录,然后再执行“npm install formidable”,项目目录下面就会生成一个node_modules子目录,里面是安装好了的formidable模块。现在运行你写好的代码, 不会再提示“Cannot find module ‘formidable‘”了。

2、按照书上的运行的时候出现了这个错误:

技术分享

看报错的原因就是在重命名的时候有问题,原来就是node在跨分区重命名文件,会有权限问题。http://www.crifan.com/node_js_use_fs_renamesync_error_exdev_cross_device_link_not_permitted/ 作者给出了解决方法。这样就可以完美的运行了。。。

node入门开发遇到的问题

原文:http://www.cnblogs.com/duhuo/p/4441595.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!