• node.js 笔记


    安装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: 'cfsghost@gmail.com',
                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 工具

  • 相关阅读:
    打印图片的属性和实现另存图片功能以及使用numpy
    opencv3.3 安装环境教程以及实现个图片读取功能
    在linux 创建网络会话和绑定两块网卡
    安装yum仓库
    C#添加错误日志信息
    关于使用宏将csv批量转换成xls的分享
    C#关于MySQL中文乱码问题
    无法启动iis express web服务器解决
    关于关闭WPS锁屏屏保及设置电脑自动关闭显示屏及休眠的分享
    为什么QQ能上却打不开网页呢?
  • 原文地址:https://www.cnblogs.com/levy/p/nodejs-bi-ji.html
Copyright © 2020-2023  润新知