• node 工程化 web项目


    1.结构

    node_modules   ( ... )

    routers     ( main.js  )

    views    ( index.html   about.HTML  404.html )

    app.js

    package.json

    2. 文件

    package.json

    {
      "name": "wj",
      "version": "1.0.0",
      "description": "init somethings",
      "main": "index.js",
      "scripts": {
        "test": "six"
      },
      "repository": {
        "type": "git",
        "url": "1.0"
      },
      "keywords": [
        "111"
      ],
      "author": "smilesix",
      "license": "ISC",
      "dependencies": {
        "cookie-parser": "^1.4.3",
        "ejs": "^2.6.1",
        "express": "^4.16.4",
        "http-proxy-middleware": "^0.19.1",
        "multer": "^1.4.1",
        "request": "^2.88.0"
      },
      "devDependencies": {}
    }
    View Code

    app.js

    var express = require("express");
    var app = express();
    // module
    var routers = require('./routers/main');
    
    // ejs for view 
    var ejs = require('ejs');  
    app.set('view engine', 'ejs');
    app.engine('html', ejs.renderFile);
    
    routers.router(app);
    
    // all is  Hello World! ( reg )
    // app.get('/*', function(req, res){
    //     res.send("Hello World!");
    // });
    
    // not router
    // app.get('/', function(req, res){
    //     res.render('index.html');
    // });
    // app.get('/about', function(req, res){
    //     res.render('about.html');
    // });
    
    
    // 404  way1
    // app.use(function(req, res) {
    //     //res.send('my 404');
    //     res.render('404.html');
    // })
    
    
    var server = app.listen(5566, function(){
        console.log("Server is running on http://localhost:5566");
    });
    View Code

    main.js

    var router = function (app) {
        app.get('/', function (req, res) {
            res.render('../views/index.html');
        });
    
        app.get('/about', function (req, res) {
            res.render('../views/about.html');
        });
    
        // 404 way2
        app.get('*', function (req, res) {
            res.render('../views/404.html');
        });
    
    };
    
    exports.router = router;
    View Code

    index.html   about.HTML  404.html              <h1>index/about/404 </h1>

    3. 备注

    测试网址: http://localhost:5566/sdfsdfsd

    node 常用命令: 参考其他文章

    express 常用命令 :参考其他文章

  • 相关阅读:
    迭代平方根
    windows怎么进如debug调试
    wxwidgets安装环境配置
    【android官方文档】与其他App交互
    Android ImageView图片自适应
    Gridview的stretchMode详解附自动宽度
    Activity生命周期解说
    一些问题
    android 文件读取(assets)
    android:scaleType属性
  • 原文地址:https://www.cnblogs.com/justSmile2/p/10040042.html
Copyright © 2020-2023  润新知