当前位置:   article > 正文

Node框架Express搭建服务器和API

Node框架Express搭建服务器和API

新建一个空文件夹

初始化项目 yarn init -y => package.json

配置 .gitignore 文件 node_modules/

安装Express插件 yarn add express

创建 app.js 用于搭建后端服务器

```js
├── README.en.md
├── README.md
├── app.js
├── package.json
└── yarn.lock

const express = require('express')
const app = express()

// 非常简单的 API 端点
app.get('/',(req,res) => { 
	res.send('Hello World')
})

// 启动服务器监听来自指定端口 8081
app.listen(8081, () => {
	console.log('Server is running on http://localhost:8081')
})
```
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

启动服务器 nodemon app.js

在浏览器中访问路由 http://localhost:8081 页面通过res.send() 展示 'Hello World' 作为页面内容
终端会打印出 Server is running on http://localhost:8081

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/497195
推荐阅读
相关标签
  

闽ICP备14008679号