• express 热启动 静态文件部署 跨域解决 调试


    1.热启动

    每次修改app.js文件,都得重新启动项目,十分不方便。这里可以用hotnode插件实现热启动

    安装:$ npm install -g hotnode

    启动项目:$ hotnode app.js

    2.静态文件部署

    app.use(express.static(__dirname + '/src/static'));指定了src目录下的static为静态目录,__dirname 是app.js的绝对路径 node自带的

    这样可以直接访问到静态文件 http://localhost:8081/aaa.js 其中aaa.js是放在src目录下的static的。

    可以指定多个目录为静态文件夹 如:

    app.use(express.static(__dirname + '/src/static1'));

    app.use(express.static(__dirname + '/src/static2'));

    3.跨域

    安装:$ npm install express-http-proxy --save

    使用:

    var proxy = require('express-http-proxy');
    var app = require('express')();
    app.use('/proxy', proxy('www.google.com'));

    4.调试

    使用命令$ npm install -g node-inspector 安装。安装后就可以使用了。

    启动调试项目 $ supervisor --debug app.js

    再打开一个控制面板 启动调试监控 $ node-inspector -p 8081 这时cmd输出的“http://127.0.0.1:8081/?port=5858”就是调试网址

    复制到浏览器 打断点跟js一样

    付上我的app.js文件

    //demo.js 文件
    var express = require('express'),
        hbs = require('hbs'),
        cookieParser = require('cookie-parser'),
        session = require('express-session'),
        fs = require('fs'),
        http = require('http'),
        app = express();
    
    app.use(cookieParser());
    app.use(session({
        secret: '123456',
        cookie: {maxAge: 60 * 1000 * 30},
        resave: false,
        saveUninitialized: true
    }));
    
    app.use(express.static(__dirname + '/src/static'));
    
    app.get('/hello', function (req, res) {
        res.send('HELLO');
    });
    
    var user = require(__dirname + '/src/server/user.js');
    app.get('/login', function (req, res) {
        if (req.session.sign) {//检查用户是否登录
            res.send('welecome <strong>' + req.session.name + '</strong>, 欢迎你再次登录');
        } else {
            user.login('王冲', '111qqq',function(userInfo){
                req.session.sign = true;
                req.session.name = userInfo.userName;
                res.send('欢迎登录,' + req.session.name);
            });
        }
    });
    
    //app.get('/index', function (req, res) {
    //    console.log(__dirname);
    //    res.sendFile(__dirname + '/src/views/' + 'index.html');
    //});
    
    app.get('/process_get', function (req, res) {
        var response = {
            firstName: req.query.firstName,
            lastName: req.query.lastName
        }
        console.log(response);
        res.end(JSON.stringify(response));
    });
    
    hbs.registerPartials(__dirname + '/src/views/partials');
    app.set('view engine', 'hbs');
    app.set('views', __dirname + '/src/views/');
    
    app.get('/news', function (req, res) {
        var userInfo = {
            userName: req.session.name
        }
        res.render('index', {title: 'hbs news demo', author: 'qujh',userInfo:userInfo});
    });
    
    
    app.get('/about/ygt', function (req, res) {
        res.render('about/test', {title: 'hbs ygt demo', author: 'qujh'});
    });
    
    app.get('/about/about', function (req, res) {
        res.render('about/about', {title: 'hbs about demos', author: 'qujh'});
    });
    
    var server = app.listen(8081, function () {
        var host = server.address().address
        var port = server.address().port
        console.log("应用实例,访问地址为 http://%s:%s", host, port);
    });

    附上配置文件 package.json

    {
      "name": "expressgulp",
      "version": "1.0.0",
      "description": "express nodeJs hbs模板",
      "main": "app.js",
      "dependencies": {
        "amui-hbs-helper": "^2.2.0",
        "body-parser": "^1.17.1",
        "browser-sync": "^2.18.8",
        "cookie-parser": "^1.4.3",
        "express": "^4.15.2",
        "express-session": "^1.15.2",
        "fs": "0.0.1-security",
        "gulp": "^3.9.1",
        "gulp-clean": "^0.3.2",
        "gulp-replace": "^0.5.4",
        "gulp-replace-task": "^0.11.0",
        "gulp-seajs-concat": "^1.0.5",
        "gulp-seajs-transport": "^0.4.0",
        "gulp-uglify": "^2.1.2",
        "handlebars": "^4.0.8",
        "hbs": "^4.0.1",
        "jquery": "^3.2.1",
        "merge-stream": "^1.0.1",
        "multer": "^1.3.0",
        "seajs": "^3.0.2",
        "session": "^0.1.0",
        "yargs": "^7.0.2"
      },
      "devDependencies": {
        "http": "0.0.0"
      },
      "scripts": {
        "注释":"npm run test以debug形式启动,另打开cmd命令node-inspector -p 8081 启动断点调试命令",
        "test": "supervisor --debug app.js",
        "dev": "node app.js"
      },
      "keywords": [
        "express",
        "hbs"
      ],
      "author": "qjh",
      "license": "ISC"
    }
  • 相关阅读:
    设计模式六大原则(PHP)
    composer update 或者 composer install提示killed解决办法
    运行composer出现do not run Composer as root/super user!
    composer install报错intervention/image 2.4.x-dev requires ext-fileinfo * -> the requested PHP extension fileinfo is missing from your system.
    laravel执行数据库迁移的过程中出现IlluminateDatabaseQueryException : SQLSTATE[HY000] [2002] Operation timed out (SQL: select * from information_schema.tables where table_schema = shop and table_name = migrations
    docker删除mysql镜像失败Error response from daemon: conflict: unable to delete 8809d5286227 (must be forced)
    测试开发岗,面试必看
    Python遍历字典去key和value需要注意的点→enumerate和items()
    Python→整数反转
    我去阿里、头条、京东的面试经(测开)
  • 原文地址:https://www.cnblogs.com/juexin/p/6831392.html
Copyright © 2020-2023  润新知