Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
请使用Node6以上版本,获得更好的性能与ES6支持。 启用Babel-es2017,不再对一些Node6已支持的特性转码。
因为Koa2已经在蓬勃发展中,Async/Await能切实的避免回调地狱。所以基于Koa2订制了一套模板。
全局安装koa2-easy,之后使用koa2命令即可在当前目录一键生成服务端程序。地址为:当前目录/koa2-easy
npm i koa2-easy -g
koa2
cd koa2-easy && npm i (推荐使用cnpm)
直接运行run.js即可
因为Koa2使用babel和ES7的Async/Await,所以需要使用babel转译启动,所以必须运行run.js,而非app.js。
node run.js
检测到文件变动,会自动重启服务器
npm i
npm run dev
npm i pm2 -g
npm run pm2
PORT=8000 npm run dev
PORT=8000 npm run pm2
把8000替换成任何你想使用的端口即可
推荐使用VeryNginx实现。
具体配置(需要先安装VeryNginx): 先配置一个 Matcher : host=app.xxxx.com ,用来提取出来所有访问 host 为 app.xxxx.com 的请求 然后配置 Proxy Pass 把这个请求转发到 127.0.0.1:3000 就可以了
需要具有ES6基础。
// router/index.js
router
.get('/', async ctx => {
// 模板渲染,第一个参数为模板名称
// 模板放置于views文件夹中
ctx.body = ctx.render('index', { title: 'Koa2-Easy' })
})
// router/index.js
router
.get('/index', async (ctx, next) => {
// 发送静态文件
await ctx.send(ctx, 'index.html', { root: 'static/index' })
})
把HTML文件放入static文件夹中。单独开辟文件夹。
如首页index.html,则放入/static/index文件夹。引用CSS等文件时,需加入文件夹名称。
<!-- /static/index/index.html -->
<link rel="stylesheet" type="text/css" href="/index/css/index.css">
直接将ctx.body设置为json格式即可
// router/test.js
router
.get('/json', (ctx, next) => {
ctx.body = { test: 'json' }
})
ctx.request.body就是post发送的数据
// router/test.js
router
.post('/post', (ctx, next) => {
ctx.body = ctx.request.body
})
按照router文件夹中范例编写,并在app.js中添加即可。
// 添加/api路由
// 将api前缀设置为/api
const router = new Router({
prefix: '/api'
})
// 对Api进行操作
router
.get('/', (ctx, next) => {
ctx.body = "This is Api page"
})
// 在app.js中引用
import api from './router/api'
app.use(api.routes())
FAQs
Easy way to use koa2
The npm package koa2-easy receives a total of 2 weekly downloads. As such, koa2-easy popularity was classified as not popular.
We found that koa2-easy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.