• 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;
    

      

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

  • 相关阅读:
    打印出1-10000之间的所有对称数(如121,1331,2442)
    代码块
    javascript判断数据类型
    块和内嵌
    xhtml+css基础知识2
    xhtml+css基础知识1
    清除浮动
    margin注意问题
    javascirpt 闭包
    css3 box-sizing属性
  • 原文地址:https://www.cnblogs.com/yiyi17/p/node.html
Copyright © 2020-2023  润新知