在节点js中运行简单服务器时抛出错误异常?

时间:2016-04-23 18:09:29

标签: javascript node.js express server client

运行一个简单的Web服务器,执行基本的发送和接收操作。这个基本服务器文件的主要目的是在某个端口上启动服务器的列表,我们必须将客户端连接到此端口,显示Hello World消息。但是当我运行该文件时出现 throw 错误。代码如下所示

var db, http, onRequest ,portnum, restFunc ,returncode;

db = require('db');
db.init();

portnum =3000;

//rest function goes here

onRequest =function (req , res) {
    var bodyStr ='';
    //function is called for each chunck of data received
    req.on('data',function(chunck) {
        bodyStr += chunck.toString();
    });

    //function is called when all the data has been received
    //request has been sent now build respons   
    req.on('end',function() {
        var argArray ,buflen ,contenttype ,outobj ,outputStr,recid;

        outputStr = "Hello World<br>\n";
        outputStr +="method:" +req.method +"\n";
        outputStr += "Posted data is: " + bodyStr + "\n";

        res.end(outputStr);
    });
}





http = require("http");
http.createServer(onRequest).listen(portnum);

console.log("Server started ,listening on port#" +portnum);

错误日志如下

module.js:327
    throw err;
    ^

Error: Cannot find module 'db'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Users\basic_server.js:3:6)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)

发生此错误后,我尝试使用npm安装缺少的 db 包,但错误仍然相同。

C:\Users\Sai Kiran>npm install db
db@1.0.6 node_modules\db
├── ds@1.2.0
├── redis@0.6.7
├── mysql@0.9.4 (hashish@0.0.4)
└── aws-sdk@0.9.7-pre.8 (xmlbuilder@8.2.2, xml2js@0.2.4)

0 个答案:

没有答案