安装nodejs
nodejs.org
Node.js for Mac 安装
安装npm
curl http://npmjs.org/install.sh | sh
更新npm
sudo npm update npm -g
安装 PM2
npm install pm2 -g
更新PM2
pm2 update
安装 express
$ npm install -g express-generator #需先安装express-generator
$ npm install -g express
$ express -V #验证是否安装成功
安装restify
将restify安装到目录restify-dmeo
$ mkdir restify-demo
$ cd restify-demo
$ npm install restify
安装时出错 no such file or directory, open ‘/Users/XXX/package.json‘
此提示在此时测试没有影响.
编写测试js
cd Desktop/
vi test.js
var restify = require(‘restify‘); //引入包
function respond(req, res, next) {
res.send(‘hello ‘ + req.params.name);
next();
}
var server = restify.createServer();
server.get(‘/hello/:name‘, respond); //访问的路径
server.head(‘/hello/:name‘, respond);
server.listen(8080, function() { //8080 端口
console.log(‘%s listening at %s‘, server.name, server.url);
});
pm2 start test.js -i 1 --name=test -f
pm2 stop test.js
安装MongoDb
brew install mongodb
npm link mongodb
npm install node-gyp -g && npm cache clean && rm -rf node_modules && npm install
启动MongoDb
mongod —config /usr/local/etc/mongod.conf
NodeJS 使用UUID
UUID,必需先为 NodeJS 安装模块 node-uuid:
npm install uuid
然后生成 UUID 并在 insert 时使用:
var uuid = require(‘node-uuid‘);
连接MongoDB
var mongodb = require(‘mongodb‘);
var mongodbServer = new mongodb.Server(‘localhost‘, 27017, { auto_reconnect: true, poolSize: 10 });
var db = new mongodb.Db(‘mydb‘, mongodbServer);
/* open db */
db.open(function() {
/* Select ‘contact‘ collection */
db.collection(‘contact‘, function(err, collection) {
/* Insert a data */
collection.insert({
name: ‘Fred Chien‘,
email: ‘[email protected]‘,
tel: [
‘0926xxx5xx‘,
‘0912xx11xx‘
]
}, function(err, data) {
if (data) {
console.log(‘Successfully Insert‘);
} else {
console.log(‘Failed to Insert‘);
}
});
/* Querying */
collection.find({ name: ‘Fred Chien‘ }, function(err, data) {
/* Found this People */
if (data) {
console.log(‘Name: ‘ + data.name + ‘, email: ‘ + data.email);
} else {
console.log(‘Cannot found‘);
}
});
});
});
Mongous - 一个轻量级的nodejs mongodb驱动
https://github.com/amark/mongous
sublime Text 编写js 工具
时间: 2024-10-05 18:03:51