美文网首页
Express - get/post/upload/cookie

Express - get/post/upload/cookie

作者: w_wx_x | 来源:发表于2019-04-16 11:56 被阅读0次

    文章推荐

    Express-官方文档
    Express-菜鸟教程
    阮一峰博客-Express
    案例代码github

    get

    文件目录.png
    <!-- index.html -->
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
    </head>
    <body>
        <form action="http://127.0.0.1:8081/process_get" method="GET">
            First Name:<input type="text" name="first_name"><br/>
            Last Name:<input type="text" name="last_name">
            <input type="submit" value="submit">
        </form>
    </body>
    </html>
    
    // server.js
    var express = require('express')
    var app = express()
    
    app.get('/index.html',function(req,res){
        res.sendFile(__dirname + '/' + 'index.html');
    })
    
    app.get('/process_get',function(req,res){
        var response = {
            "first_name":req.query.first_name,
            "last_name":req.query.last_name
        }
        console.log(response)
        res.end(JSON.stringify(response))
    })
    
    var server = app.listen(8081,function(){
        console.log(server.address())
    })
    
    node server.js
    

    post

    文件目录.png
    <!-- index.html -->
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
    </head>
    <body>
        <form action="http://127.0.0.1:8081/process_post" method="POST">
            First Name:<input type="text" name="first_name"><br/>
            Last Name:<input type="text" name="last_name">
            <input type="submit" value="submit">
        </form>
    </body>
    </html>
    
    // server.js
    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
     
    // 创建 application/x-www-form-urlencoded 编码解析
    var urlencodedParser = bodyParser.urlencoded({ extended: false })
     
     
    app.get('/index.html', function (req, res) {
       res.sendFile( __dirname + "/" + "index.html" );
    })
     
    app.post('/process_post', urlencodedParser, function (req, res) { 
       // 输出 JSON 格式
       var response = {
           "first_name":req.body.first_name,
           "last_name":req.body.last_name
       };
       console.log(response);
       res.end(JSON.stringify(response));
    })
    var server = app.listen(8081,function(){
        console.log(server.address())
    })
    
    node server.js
    
    效果图.gif

    upload

    <!-- index.html -->
    <html>
    <head>
        <title>文件上传表单</title>
    </head>
    <body>
        <h3>文件上传:</h3>
        选择一个文件上传: <br />
        <form action="/file_upload" method="post" enctype="multipart/form-data">
            <input type="file" name="image" size="50" />
            <br />
            <input type="submit" value="上传文件" />
        </form>
    </body>
    </html>
    
    // server.js
    // 访问 http://127.0.0.1:8081/index.htm
    
    var express = require('express');
    var app = express();
    var fs = require("fs");
    
    var bodyParser = require('body-parser');
    var multer = require('multer');
    
    app.use('/public', express.static('public'));
    app.use(bodyParser.urlencoded({
        extended: false
    }));
    app.use(multer({
        dest: '/tmp/'
    }).array('image'));
    
    app.get('/index.html', function (req, res) {
        res.sendFile(__dirname + "/" + "index.html");
    })
    
    app.post('/file_upload', function (req, res) {
    
        console.log(req.files[0]); // 上传的文件信息
    
        var des_file = __dirname + "/" + req.files[0].originalname;
        fs.readFile(req.files[0].path, function (err, data) {
            fs.writeFile(des_file, data, function (err) {
                if (err) {
                    console.log(err);
                } else {
                    response = {
                        message: 'File uploaded successfully',
                        filename: req.files[0].originalname
                    };
                }
                console.log(response);
                res.end(JSON.stringify(response));
            });
        });
    })
    var server = app.listen(8081, function () {
        console.log(server.address())
    })
    

    cookie

    // cookie.js
    /* 利用中间件向Node.js服务器发送cookie信息
       执行node express.js
       访问http://127.0.0.1:8081查看终端信息输出
    */
    
    var express = require('express')
    /* cookie-parser
            cnpm i cookie-parser --save
            是一个解析Cookie的工具,通过req.cookies可以取到传过来的cookie,并将其转换为对象
    */
    var cookieParser = require('cookie-parser')
    var util = require('util')
    
    var app = express()
    app.use(cookieParser())
    
    app.get('/',function(req,res){
        console.log("Cookies:" + util.inspect(req.cookies))
    })
    
    app.listen(8081)
    
    node cookie.js
    

    相关文章

      网友评论

          本文标题:Express - get/post/upload/cookie

          本文链接:https://www.haomeiwen.com/subject/nzvvwqtx.html