0
点赞
收藏
分享

微信扫一扫

Nodejs轻松入门(四)Express

目录

一.Express环境搭建

二. 路由

1.postman

 2.模块化

三.托管静态文件

四.GET与POST传递参数

五.使用中间件


一.Express环境搭建

在项目目录下执行:

cnpm install express --save

添加Express框架代码提示:

当前项目目录下执行(即只在最外层的文件下安装各种模块即可,这样项目文件夹下的其他文件夹里的文件也可以使用这些模块功能!)

npm i -D @types/express

Express版Hello world:

index.js:

const express=require("express");
//构建express对象
const app=express();

//配置服务器访问地址(即路由的概念)
app.get("/list",function(req,res){
    //发送数据不需要格式转换,很方便
    res.send({
        status:200,
        results:[
            {
                name:"kd",
                age:"20"
            },
            {
                name:"jack",
                age:"30"
            }
        ]//results
    })//send
})

app.listen(3000,function(){
    console.log("服务器运行在3000端口上");
})

输入:http://localhost:3000/list

输出:

二. 路由

1.postman

利用postman进行访问(主要是方便查看post请求的结果)

安装地址:Download Postman | Get Started for Free

会自动安装到C盘,根据桌面图标把C盘的Postman文件夹(即自动安装的文件夹)剪切到想存放的路径即可。

const express=require("express");
//构建express对象
const app=express();

//配置服务器访问地址(即路由的概念)
//以下编写函数方式是ES6新特性
app.get("/list",(req,res) => {
    //发送数据不需要格式转换,很方便
    res.send({
        status:200,
        info:"测试(get方式)"
    })//send
})

app.post("/login",(req,res)=>{
    res.send({
        msg:"登录成功(post方式)"
    })
})

app.listen(3000,function(){
    console.log("服务器运行在3000端口上");
})

 

 2.模块化

index.js:

const express=require("express");
const router = require("./router");
//构建express对象
const app=express();

//第一个参数是规定地址必须有的前缀
app.use("/api",router);

app.listen(3000,function(){
    console.log("服务器运行在3000端口上");
})

router.js:

const express=require("express");
//创建路由对象
const router=express.Router();

//配置服务器访问地址(即路由的概念)
//以下编写函数方式是ES6新特性
router.get("/list",(req,res) => {
    //发送数据不需要格式转换,很方便
    res.send({
        status:200,
        info:"测试(get方式)"
    })//send
})

router.post("/login",(req,res)=>{
    res.send({
        msg:"登录成功(post方式)"
    })
})

module.exports=router;

三.托管静态文件

例如当我们访问图片时,很多图片并不是写死在前端的,而是存在服务器当中,所以这时就需要静态托管文件。

Express静态托管戳这里

const express= require("express");
const app=express();

//静态文件托管
//public是自定义名称,和对外开放的那个文件夹名称一样就行
app.use(express.static('public'));

app.get("/banner",(req,res)=>{
    res.send({
        code:200,
        result:{
            id:1001,
            name:"nodejs",
            image:"http://localhost:3000/image/auto.jpg"
        }
    })
})

app.listen(3000,()=>{
    console.log(3000);
})

输出:

Express 在静态目录查找文件,因此,存放静态文件的目录名不会出现在 URL 中。所以在路径输入时不用写上public。

image的链接地址可以点击跳转到图片,如果不规定静态文件托管,那么这条地址不能跳转。

四.GET与POST传递参数

const express=require("express");
const app=express();
const url=require("url");
const bodyParser=require("body-parser");

//使能接受post参数
app.use(bodyParser.urlencoded({
    extended:true
}))

//接受参数 user=?
app.get("/list",(req,res)=>{
    const user=url.parse(req.url,true).query.user;
    console.log(user);
    if(user==="kd"){
        res.send({
            msg:user
        })
    }else{
        res.send({
            msg:"get测试"
        })
    }
})

app.post("/login",(req,res)=>{
    const username=req.body.username;
    const password=req.body.password;
    console.log(username,password);
    if(username==="kd"&&password==="123"){
        res.send({
            msg:"登陆成功!"
        })
    }else{
        res.send({
            msg:"用户名或密码错误"
        })
    }
})

app.listen(3000,()=>{
    console.log(3000);
})

输出:

GET方式:

POST方式:

五.使用中间件

这里涉及了连接数据库和apache等技术,先跳过,之后再回来补充!敬请期待!

举报

相关推荐

0 条评论