Express (nodejs) 相关

Express (nodejs) 相关

安装 express-generator 脚手架,新建项目

  1. 执行命令

npm install express express-generator@4 -g

同时安装 express,和 express 脚手架.

javascript 复制代码
npm install express express-generator@4 -g
  1. 通过 express 脚手架创建 express 项目

express --no-view iike-api

cd iike-api

npm install

npm start (启动项目)

指令解释:

  • --no-view 表示 不需要视图模板
  • iike-api 表示 express 项目名称
  • cd iike-api 进入 iike-api 项目中
javascript 复制代码
express --no-view iike-api

cd iike-api

npm install

npm start
  1. 访问本地地址

http://localhost:3000/

页面显示 Express Welcome to Express ,表示项目启动成功

JSON 格式配置

  1. 在 express 项目启动成功后,访问本地地址;会发现,是一个 html 格式.
  2. express 项目一般用来做接口使用,所以我们需要将 html 格式转化为 json 格式.
  3. 操作如下:
  1. iike-api\routes\index.js 修改下面代码
javascript 复制代码
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});

// 替换为
router.get("/", function (req, res, next) {
  res.json({ message: "Hello Nodejs!" });
});
  1. 删除 iike-api\public\index.html 文件

  2. 重启 express 服务

    ctrl + c 终止, npm start 重启.

  3. 刷新页面后,拿到的信息就是 json 格式的了

推荐浏览器处理 JSON 格式的插件

JSON-handle ,

  • edge 浏览器可以直接在应用商店中下载

安装 nodemon 解决频繁重启问题

  1. 下载 nodemon 依赖
javascript 复制代码
npm i nodemon
  1. 配置 package.json 文件. 将启动脚本 node ./bin/www 改为 nodemon ./bin/www
javascript 复制代码
  "scripts": {
    "start": "nodemon ./bin/www"
  },
  1. 第一次配置,记得重启服务

项目结构 与 代码解析

  1. routes 文件夹(重要),不同网址对应要不同的程序.后续开发基本都会在 routes 中进行
  2. app.js 配置跨域 等配置,都会在这里

使用 Docker 运行 mysql

  1. 安装 docker docker 官网下载
相关推荐
Dxy123931021611 小时前
JS DES 加密详解
开发语言·javascript·arcgis
非科班Java出身GISer3 天前
ArcGIS JS 基础教程(46):编辑要素属性 FeatureForm
arcgis·arcgis js编辑要素·arcgis js 要素·arcgis js要素属性·arcgis js 编辑属性·arcgis js编辑要素属性
梅如你4 天前
ISPRS | 1990-2020年间中国人工林与天然林的空间分布数据
arcgis·gis·遥感
limit00757 天前
公开 WMTS / XYZ 瓦片接入 Cesium:6 类常见问题与排查顺序
chrome·低代码·arcgis·web3·旅游
星光开发者8 天前
基于springboot智慧草场牛羊管理系统-计算机毕设【课程设计】50360
vue.js·spring boot·后端·mysql·php·课程设计·express
limit00758 天前
三维模型悬空与切片偏移的排查顺序
低代码·3d·arcgis·web3·安卓·webgl
最强小杰8 天前
Continue + minimax-m3 接入教程:协议、模型 ID、404 与工具调用排错
arcgis·ai
vx_Biye_Design12 天前
django房屋租赁推荐系统78455-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·课程设计·express
2501_9343044012 天前
新增2026年! 2012-2026年全国逐月夜间灯光数据
arcgis·gis数据
梅如你13 天前
中国2026年路网数据
arcgis·gis·遥感