const Koa = require('koa')
const app = new Koa()
app.use(async ctx => {
ctx.body = 'Hello Koa2'
})
app.listen(3000)
console.log('server is running at port 3000')
const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
// 路由
let router = new Router({
// 前缀
prefix : '/nodejs'
})
router
.get('/index', async ctx => {
let html = '<a href="https://www.abc.edu.cn">商贸学院</li>'
ctx.body = html
})
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000)
console.log('server is running at port 3000')
const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
// 子路由
let index = new Router()
index.get('/index', async ctx => {
let html = '<a href="https://www.abc.edu.cn">商贸学院</li>'
ctx.body = html
})
// 子路由
let error = new Router()
error.get('/error', async ctx => {
let html = 'Error Page'
ctx.body = html
})
// 装载路由
let router = new Router()
router.use(index.routes(), index.allowedMethods()) // 访问路径:/index
router.use('/koa2', error.routes(), error.allowedMethods()) // 访问路径:/koa2/error
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000)
console.log('server is running at port 3000')
const Koa = require('koa')
const app = new Koa()
const bodyParser = require('koa-bodyparser')
// 使用ctx.body解析中间件
app.use(bodyParser())
app.use(async ctx => {
// GET请求
if (ctx.url == '/' && ctx.method == 'GET') {
let html = `
<form method="POST" action="/">
用户: <input name="username" /><br/>
密码: <input name="password" type="password"/><br/>
<input type="submit"/>
</form>
`
ctx.body = html
}
// POST请求
else if (ctx.url == '/' && ctx.method == 'POST') {
let postData = ctx.request.body
ctx.body = postData
}
else {
ctx.body = '<h1>404 Page Not Found</h1>'
}
})
app.listen(3000)
console.log('server is running at port 3000')
const Koa = require('koa')
const app = new Koa()
app.use(async ctx => {
// GET请求
let url = ctx.url
let ctx_query = ctx.query
let ctx_querystring = ctx.querystring
ctx.body = {
url,
ctx_query,
ctx_querystring
}
})
app.listen(3000)
console.log('server is running at port 3000')
const Koa = require('koa')
const path = require('path')
const static = require('koa-static')
const app = new Koa()
// 静态资源目录对于相对入口文件的路径
const staticPath = './static'
app.use(static(
path.join( __dirname, staticPath)
))
app.use( async ctx => {
let html = 'YungFan'
ctx.body = html
})
// 访问:http://localhost:3000/iOSer.png http://localhost:3000/style.css http://localhost:3000/method.js
app.listen(3000)
console.log('server is running at port 3000')
const Koa = require('koa')
const fs = require('fs')
const path = require('path')
const app = new Koa()
const Router = require('koa-router')
const koaBody = require('koa-body')
// koaBody 完成上传文件
app.use(koaBody({
multipart: true,
formidable: {
maxFileSize: 5 * 1024 * 1024 // 设置上传文件大小最大限制,默认20M,这里设置为5M
}
}))
app.use(async ctx => {
// 单文件
if (ctx.url == '/upload' && ctx.method == 'GET') {
let html = `
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="file" id="file" value=""/><br/>
<input type="submit"/>
</form>
`
ctx.body = html
}
// 多文件
else if (ctx.url == '/uploads' && ctx.method == 'GET') {
let html = `
<form method="POST" action="/uploads" enctype="multipart/form-data">
<input type="file" name="file" id="file" value="" multiple="multiple" /><br/>
<input type="submit"/>
</form>
`
ctx.body = html
}
else if (ctx.url == '/upload' && ctx.method == 'POST') {
// 上传单个文件
const file = ctx.request.files.file // 获取上传文件
// 创建可读流
const reader = fs.createReadStream(file.path)
// 上传路径
let filePath = path.join(__dirname, 'upload/')
// 检查路径是否存在
checkDirExist(filePath)
// 文件名
const fileName = `${filePath}/${file.name}`
// 创建可写流
const upStream = fs.createWriteStream(fileName)
// 可读流通过管道写入可写流
reader.pipe(upStream)
return ctx.body = "单文件上传成功!"
}
else if (ctx.url == '/uploads' && ctx.method == 'POST') {
// 上传多个文件
const files = ctx.request.files.file
for (let file of files) {
// 里面操作和前面单文件一样
const reader = fs.createReadStream(file.path)
let filePath = path.join(__dirname, 'upload/')
checkDirExist(filePath)
const fileName = `${filePath}/${file.name}`
const upStream = fs.createWriteStream(fileName)
reader.pipe(upStream)
}
return ctx.body = "多文件上传成功!"
}
else {
ctx.body = '<h1>404 Page Not Found</h1>'
}
})
app.listen(3000)
console.log('server is running at port 3000')
// 判断文件夹是否存在,不存在则创建
function checkDirExist(path) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path)
}
}
const mysql = require('mysql')
let pools = {} // 连接池
let operation = (sql, host = '127.0.0.1') => {
if (!pools.hasOwnProperty(host)) { // 是否存在连接池
pools[host] = mysql.createPool({ // 不存在创建
host: host,
port: '3306',
user: 'root',
password: 'root',
database: 'nodejsDB'
})
}
return new Promise((resolve, reject) => {
pools[host].getConnection((err, connection) => { // 初始化连接池
if (err)
console.log(err,'数据库连接失败')
else
connection.query(sql, (err, results) => { // 去数据库查询数据
connection.release() // 释放连接资源
if (err)
reject(err)
else
resolve(results)
})
})
})
}
module.exports = operation
const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
const dbTool = require('./dbTools') // 注意路径
let router = new Router()
router.get('/users/list', async (ctx, next) => {
await dbTool(`select * from t_user`).then((res) => {
console.log(res)
ctx.body = res // 返回给前端的数据
})
await next()
})
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000)
console.log('server is running at port 3000')