首页 > 编程知识 正文

nodejs文件追加内容(nodejs 文档)

时间:2023-12-17 12:25:55 阅读:316538 作者:QFOX

本文目录一览:

求教,nodejs如何往mongoDB中批量插入数据

nodejs把数据存到mongodb里方法:

首先,创建一个数据库保存用户信息。

在这个数据库中创建一个名为 users 的集合,并插入一条用户信息。当前没有 users 集合,mongodb 会直接创建它。

db.users.insert( { "userId":1, "name":"tom", "email":"tom@nodejs.org" })

查找信息可以使用 find 或者 findOne,区别在于 findOne 只会返回一个结果。

db.users.findOne( {"userId": 1})

返回的结果:

{

"_id" : ObjectId("5413be6e9e1c9f9c4386756d"),

"userId" : 1,

"name" : "tom",

"email" : "tom@nodejs.org"

}

驱动程序

编辑 package.json, 添加对于 mongodb 的引用。

{

"name": "express-api",

"version": "0.0.1",

"dependencies": {

"express": "2.5.9",

"ejs": "0.4.2",

"mongodb": "1.4.1"

}

}

重新 npm install 安装 mongodb 的驱动。

使用 MongoDB 数据库

修改代码,首先 require mongodb 模块,然后连接到 mongodb 数据库。

var mongo = require("mongodb");

var express = require("express");

var app = express.createServer();

app.set("view engine", "ejs");

app.set("views", __dirname + "/views");

app.set("view options", { layout: false });

app.get("/", function (request, response) {

response.render("index");

});

app.get("/user/:id", function (request, response) {

var id = request.params.id;

console.log(id);

app.users.findOne({ "userId": +id }, function (error, doc) {

if (error) return next(error);

response.json(doc);

});

});

// connect mongodb

var server = new mongo.Server("127.0.0.1", 27017);

var db = new mongo.Db("members", server, {safe:true }).open(function (error, client) {

if (error) throw error;

console.log("33[96m + 33[39m connected to mongodb");

app.users = new mongo.Collection(client, "users");

client.ensureIndex("users", "userId", function (error) {

if (error) throw error;

console.log("33[96m + 33[39m ensured index.");

console.log("Web Server listening ......");

app.listen(3000);

});

});

注意现在是到数据库中查找用户。id 前面的 + 用来将表单中的字符串类型数据转换为需要的数字类型。

app.users.findOne({ "userId": +id }, function (error, doc) {

if (error) return next(error);

response.json(doc);

});

如何加载Nodejs模块

nodejs的几种模块加载方式

一.直接在exports对象中添加方法

1. 首先创建一个模块(module.js)module.js

exports.One = function(){

console.log('first module');

};

2.load.jsvar module =require('./module');

module.One();

这样我们就可以在引入了该模块后,返回一个exports对象,这里是指module对象,其实都只是两个引用或者句柄,只是都指向了同一个资源,在load.js里,module的名字可以是任意取的,因为它仅仅是指向require('./module');返回后的一个实例对象的引用,在load.js文件里的module和在module.js里的exports对象是同一个东西.因此上述两个文件可以用一个文件来表示:exports.One = function(){

console.log('first module');

};

exports.One();

其运行结果是一致的,这里我们可以很清晰的看到,我们在使用require('./xxxx')后其实返回的总是在 xxxx.js文件中的exports对象的引用,这个引用的名字我们可以任意取,但是为了规范我们还是最好取符号某些非标准规定(后面说道),但是这样会有不妥的地方,因为它是始终指向exports的实例对象,也就是说,我们虽然有了这个模块,但是这个模块我们只能使用一次,这取决于rquire('./module')只会加在一次该模块.比如我们修改上述代码,

module.js

var name ;

exports.setName = function(oName){

name = oName;

};

exports.getName = function(){

console.log(name);

};

load.jsvar module1 = require('./module');

module1.setName("felayman1");

module1.getName();

var module2 = require('./module');

module2.setName("felayman2");

module2.getName();

module1.getName();

我们可以看到,虽然我们使用了两次require('./module');,但是当我们修改module2后,module1的内容也被修改,这恰恰说明了,module1和module2是指向的同一个对象.有时候这并不影响我们的程序,但是如果我们的module是Person呢?我们希望我们require('./person')后返回的是不同的对象.因此,这种方式是有缺陷的,尽管很方便,这种方式在大部分nodejs的模块中都是很常见,比如fs模块,http模块等.

二.将模块中的函数挂载到exports对象的属性上

person.js

function Person{

var name;

this.setName = function(theName){

name = theName;

};

this.sayHello = function(){

console.log('Hello',name);

};

}

exports.Person = Person;

load.js

var Person = require('./person').Person;

var person1 = new Person();

person1.setName("felayman1");

person1.sayHello();

var person2 = new Person();

person2.setName("felayman2");

person2.sayHello();

person1.sayHello();

这样我们可以看到,我们就可以引入一个函数了,我们把在person.js文件中的Person函数设置为eports对象的一个属性,我们只需要在load.js文件中引入该属性,就可以获取到多个该函数的实例,在nodejs中的EventEmitter就是基于这种方式,但是这样我们总是在使用 require('./person').Person;这样的写法有点太复杂,因此nodejs允许我们使用其他更简洁的方式,利用全局变量--module,这样我们在其他文件中引入其他模块的时候,就更方便了.

三.利用全局变量module

person.js

function Person(){

var name;

this.setName = function(theName){

name = theName;

};

this.sayHello = function(){

console.log('Hello',name);

};

}

// exports.Person = Person;

module.exports = Person;

load.jsvar Person = require('./person');

var person1 = new Person();

person1.setName("felayman1");

person1.sayHello();

var person2 = new Person();

person2.setName("felayman2");

person2.sayHello();

person1.sayHello();

这样一修改,我们就在使用require函数的时候就方便了,如果觉得这里难以理解,我们可以把两个文件里语法放到一起:var Person = require('./person');

module.exports = Person;

这样,我们就可以看出,其实就是这样var Person = Person.

因为上述我们都已经说过,require('./person')其实就是module.exports 对象的,这里的module我们不用太在意,就跟javascript中的window一样,是一个全局变量,即 module.exports =exports就类似于window.alert() =alert()差不多的效果,这样我们就能看出,我们再次使用require('./person')的时候其实就是导入了我们所需要的exports对象的属性函数模板了,这样我们也可以多次实例化我们所需要的对象了.这种方式是综合了前两种的方法,因此也是官方推荐的使用方法.

Nodejs 如何文件追加从最开始开始追加

var http = require("http"),

url = require("url"),

path = require("path"),

fs = require("fs");

http.createServer(function (req, res) {

var pathname=__dirname+url.parse(req.url).pathname;

if (path.extname(pathname)=="") {

pathname+="/";

}

if (pathname.charAt(pathname.length-1)=="/"){

pathname+="index.html";

}

path.exists(pathname,function(exists){

if(exists){

switch(path.extname(pathname)){

case ".html":

res.writeHead(200, {"Content-Type": "text/html"});

break;

case ".js":

res.writeHead(200, {"Content-Type": "text/javascript"});

break;

case ".css":

res.writeHead(200, {"Content-Type": "text/css"});

break;

case ".gif":

res.writeHead(200, {"Content-Type": "image/gif"});

break;

case ".jpg":

res.writeHead(200, {"Content-Type": "image/jpeg"});

break;

case ".png":

res.writeHead(200, {"Content-Type": "image/png"});

break;

default:

res.writeHead(200, {"Content-Type": "application/octet-stream"});

}

fs.readFile(pathname,function (err,data){

res.end(data);

});

} else {

res.writeHead(404, {"Content-Type": "text/html"});

res.end("h1404 Not Found/h1");

}

});

}).listen(8080, "127.0.0.1");

console.log("Server running at ");

nodejs中怎么添加数据到mysqlzhong

要创建的数据库名

TEST_DATABASE = ‘nodejs_mysql_test',

//要创建的表名

TEST_TABLE = ‘test';

//用户名

client.user = ‘root';

//密码

client.password = ‘root';

//创建连接

client.connect();

client.query(‘CREATE DATABASE ‘+TEST_DATABASE, function(err) {

if (err err.number != Client.ERROR_DB_CREATE_EXISTS) {

throw err;

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。