• express快速入门


    //express入门

    var express = require('express'); var app = express(); var bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: false }));
     //app.use('/', express.static(path.join(__dirname, 'public')));
    
    app.all('*',function(req,res,next){
        res.header('Access-Control-Allow-Origin','*');
        if(req.method=='OPTIONS') res.send(200);
        else next();
    })
    app.get('/demo',function(req,res){
        res.send({
            a:1,
            b:2
        })
    })
    var interfaces = require('os').networkInterfaces();

    var host = '';
    for(var devName in interfaces){
         var iface = interfaces[devName];
         for(var i=0;i<iface.length;i++){
             var alias = iface[i];
             if(alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal){
                host = alias.address;
             }
         }
    }
    var server = newFunction();
    function newFunction() {
        return app.listen(3000, function () {
            // var host = net.en0[1].address||'';
            // var host2 = net.lo0[0].address || ''
            // var port = server.address().port;
            var port = 3000;
            console.log("应用实例,访问地址为 http://%s:%s",host,port);
            // console.log("应用实例,也可以访问地址为 http://%s:%s", host2, port);
        });
    }
     

     express-router 的使用

    const express = require('express');
    const path = require('path');
    const favicon = require('serve-favicon');
    const routes = require('./routes/index');
    const app = express();
    
    const bodyParser = require('body-parser');//post请求查看参数需要
    app.use(bodyParser.json());//返回仅解析json
    app.use(bodyParser.urlencoded({extended: false}));//UTF-8编码
    
    app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
    
    
    app.use(express.static(path.join(__dirname,'public')));//访问静态资源,单文件
     //多文件
     const crl = require('./controller/index');
     
    app.all('*',function(req,res,next){
        res.header('Access-Control-Allow-Origin','*');
        if(req.method=='OPTIONS') res.send(200);
        else next();
    })
    routes(app);
      crl(app);
     
    
    app.listen(8808,function(){
        console.log('88080000');
    });

    index.js

    module.exports=function(app){
        app.use('/',require('./login'));
    };

    login.js

    const express = require('express');
    const router = express.Router();
    
    router.get('/loginout', function (req, res) {
        //res.clearCookie('zxdc');
        res.json({code:200,msg: '退出成功'});
    });
    
    module.exports = router;

    contrller/index.js

    module.exports=function(app){
        app.use('/',require('./test'));
    }
    

    contrller/test.js  

    const express = require('express');
    const crl = express();
    const fs = require('fs');
    var path = require('path');
    
    //方法一
    crl.use('/', express.static(path.join(__dirname, '/../public')));
    crl.use('/demo', express.static(path.join(__dirname, '/../public', 'index2.html')));
    
    //方法二
    crl.get("/helloworld.html", function (request, response) {
    	console.log(request.path)
    	//fs.readFile("../public/" + request.path.substr(1), function (err, data) {
    	// fs.readFile(path.join(__dirname, '/../public','helloworld.html'), function (err, data) {
    	 fs.readFile(__dirname+'/../public' + request.path, function (err, data) {
    	// body
    	if (err) {
    		console.log(err);
    		//404:NOT FOUND
    		response.writeHead(404, {
    			"Content-Type": "text/html"
    		});
    	} else {
    		//200:OK
    		response.writeHead(200, {
    			"Content-Type": "text/html"
    		});
    		response.write(data.toString());
    	}
    	response.end();
    });
    
    module.exports = crl;
    

      

    如果觉得文章不错,可以给小编发个红包给予鼓励.

  • 相关阅读:
    zabbix通过简单shell命令监控elasticsearch集群状态
    zabbix通过简单命令监控elasticsearch集群状态
    生产环境elasticsearch5.0.1和6.3.2集群的部署配置详解
    执行update语句mysql5.6报错ERROR 1292 (22007): Truncated incorrect DOUBLE value: '糖糖的坤大叔'
    aliyun添加数据盘后的物理分区和lvm逻辑卷两种挂载方式
    aliyun添加数据盘parted方式分区格式化和lvm挂载及数据盘的扩容
    Zabbix Agent active批量调整客户端为主动模式监控
    aliyun服务器ecs被ddos后无法被zabbix-server监控的处理
    centos环境自动化批量安装jdk软件脚本
    centos环境自动化批量安装软件脚本
  • 原文地址:https://www.cnblogs.com/yiyi17/p/node.html
Copyright © 2020-2023  润新知