Express----学习

Express

install  the Express farmwork globally using  NPM    安装Express模块:

npm install Express

install  important modules along with express   安装一些由express支持的中间件组件

npm install body-parser                            处理JSON ,Raw, Text,URL编码

npm install cookie-paeser                        请求读取cookie并在响应中设置cookie

npm install multer                                      处理 multipart/form-data

npm install basic-auth-connet                 提供对基本HTTP身份验证的支持

npm install  compression :                    提供Gzip压缩支持

Express添加到package.json模块,确保你部署应用的时候,模块被安装

124421_2gii_2426412.png

Serving Static File 静态文件服务:

Express provides a built-in middleware express-static to serve static file ,such as image,CSS,Javascript,etc.

static中间件可以直接从磁盘对客户端提供静态文件服务:express.static(path,[options])

var express=require('express');//加载experss模块
var app=express();
app.use(express.static('untitled'));//静态文件服务

index.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My blogger</title>
    <form action="http://127.0.0.1:8081/process_get"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>
<form action="http://127.0.0.1:8081/file_upload"method="POST" 
      enctype="multipart/form-data">
    File:<input type="file"name="file"size="50"/>
    <br>
    <input type="submit"value="Upload File"/>
</form>
</head>
<body>
</body>
</html>

处理GET请求:

var express=require('express');//加载experss模块
var app=express();
app.use(express.static('untitled'));//静态文件服务
app.get('/index.html',function (req,res) {//-提交/index.html get请求
    res.sendFile(__dirname+"/"+'index.html');

});
app.get('/process_get',function (req,res) {//-提交/process_get 请求
    response={
        first_name:req.query.first_name,
        last_name:req.query.last_name
    };
    console.log(response);
    res.end(JSON.stringify(response));   //JSON格式
});
var server=app.listen(8081,function () {
    var host=server.address().address;
    var port=server.address().port;
});

处理POST请求:

var express=require('express');//加载experss模块
var app=express();
var bodyParser=require('body-parser');//body-parser把post正文中的JSON数据解析为req.body属性
var urlencodedParser=bodyParser.urlencoded({extended:false});
app.use(express.static('untitled'));//静态文件服务
app.get('/index.html',function (req,res) {//-提交/index.html get请求
    res.sendFile(__dirname+"/"+'index.html');

});
app.post('/process_get',urlencodedParser,function (req,res) {
    response={
        first_name:req.body.first_name,
        last_name:req.body.last_name
    };
    console.log(response);
    res.end(JSON.stringify(response));   //JSON格式
});
/*app.get('/process_get',function (req,res) {//-提交/process_get 请求
    response={
        first_name:req.query.first_name,
        last_name:req.query.last_name
    };
    console.log(response);
    res.end(JSON.stringify(response));   //JSON格式
});*/
var server=app.listen(8081,function () {
    var host=server.address().address;
    var port=server.address().port;
});

問題:

var express=require('express');//加载experss模块
var app=express();
var fs=require('fs');
var bodyParser=require('body-parser');
var multer=require("multer");
var urlencodedParser=bodyParser.urlencoded({extended:false});

app.use(express.static('untitled'));//静态文件服务
app.use(urlencodedParser);
app.use(multer({ dest:'/vives/'}));

app.get('/index.html',function (req,res) {//-提交/index.html get请求
    res.sendFile(__dirname+"/"+'index.html');

});

app.post('/process_get',urlencodedParser,function (req,res) {
    response={
        first_name:req.body.first_name,
        last_name:req.body.last_name
    };
    console.log(response);
    res.end(JSON.stringify(response));   //JSON格式
});

app.post('/file_upload',function (req,res) {
    var file=__dirname+'/'+req.files.file.name;
    fs.readFile(req.files.file.path,function (err,data) {
       fs.write(file,data,function (err) {
           if(err){
               console.error(err);
           }else{
               response={
                   message:'File upload successfully',
                   filename:req.files.file.name
               };
           };
           console.log(response);
           res.end(JSON.stringify(response));
       }) ;
    });
});
/*app.get('/process_get',function (req,res) {//-提交/process_get 请求
    response={
        first_name:req.query.first_name,
        last_name:req.query.last_name
    };
    console.log(response);
    res.end(JSON.stringify(response));   //JSON格式
});*/
var server=app.listen(8081,function () {
    var host=server.address().address;
    var port=server.address().port;
});

module.js:328
    throw err;
    ^

Error: Cannot find module 'multer'

 使用 sudo npm install multer -g  在運行還報錯    需安裝在node_modules下 (相關路徑)

转载于:https://my.oschina.net/u/2426412/blog/804699

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值