在vue或者react中使用express框架

在react 或者 vue项目中使用express框架

1.创建vue或者 react 项目

2.在项目中创建server文件夹,创建server.js

//require()方法引入express模块
const express = require("express");
//express()方法,新建app
const app = express();
//get(url,function(req,res){})方法,设置请求返回参数和访问路径
app.get('/',function(req,res){
    //res.send(); 往页面上显示内容
    res.send("<h3>test</h3>");
});

app.get('/data',function(req,res){
    //往页面上显示json数据
    res.json({
        name:"zs",
        age:12
    });
});

//app.listen()方法,设置监听端口
app.listen(9093,function(){
    console.log("listening port 9093...");
})

3.运行node server.js

 访问localhost:9093  可以看到内容 test

 访问localhost:9093/data 可以看到json数据

4.更改了server.js, 需要重新运行。

  如果不想每次更改后,都重新运行,那么就需要安装nodemon

安装nodemon ,以管理员身份运行cmd,npm i nodemon -g 

安装后,运行nodemon server.js

猜你喜欢

转载自www.cnblogs.com/luguankun/p/10214902.html