1.0.0 • Published 2 years ago
koa-zhousan v1.0.0
koa-static
Koa static file serving middleware, wrapper for koa-send.
Installation
$ npm install koa-staticAPI
const Koa = require('koa');
const app = new Koa();
app.use(require('koa-static')(root, opts));rootroot directory string. nothing above this root directory can be servedoptsoptions object.
Options
router.post('/user/logon',indexController.Logon)
router.post('/user/sendcode',indexController.SendCode)
router.post('/user/login',indexController.Login)
router.post('/car/add',indexController.AddCar)
router.post('/car/delete',indexController.DeleteCar)
router.get('/car/show',indexController.CarShow)
router.put('/car/update',indexController.UpdateCar)maxageBrowser cache max-age in milliseconds. defaults to 0hiddenAllow transfer of hidden files. defaults to falseindexDefault file name, defaults to 'index.html'deferIf true, serves afterreturn next(), allowing any downstream middleware to respond first.gzipTry to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true.brTry to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists (note, that brotli is only accepted over https). defaults to true.- setHeaders Function to set custom headers on response.
extensionsTry to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults tofalse)
Example
const query = require('../db/query');
const md5 = require('md5')
const CheckAccount = async(params)=>{
var sql = ` select * from userinfo where account='${params.account}' or phone='${params.phone}' `
return await query(sql)
}
const AddUser = async(params)=>{
var newpassword=md5(params.password)
var sql =` insert into userinfo (account,password,phone)
value('${params.account}','${newpassword}','${params.phone}') `
return await query(sql)
}
const AddCar = async(params)=>{
var sql = ` select * from car where shopid='${params.shopid}' and userid='${params.userid}' `
var cardata = await query(sql)
if(cardata.length>0){
var sql2 = ` update car set count=count+${params.count} where shopid='${params.shopid}' and userid='${params.userid}' `
return await query(sql2)
}
var sql3 = ` insert into car (shopid,count,userid)
values('${params.shopid}','${params.count}','${params.userid}') `
return await query(sql3)
}
const DeleteCar = async (params) => {
var sql = ` delete from car where id in(${params.ids}) `
return await query(sql)
}
const CarShow = async(params)=>{
var page = params.page;
var size = params.size;
var sql = ` select b.shopname,b.price,a.count,a.count*b.price zpric,a.id from car a
join shop b on a.shopid=b.id where a.userid='${params.userid}' `
if(params.shopname){
sql+=` and b.shopname like '%${params.shopname}%' `
}
var zcount = (await query(sql)).length
sql+=` limit ${(page-1)*size},${size} `
var cardata = await query(sql)
return { cardata,zcount }
}
const UpdateCar = async(params)=>{
console.log(params);
var sql2 = ` select * from car where id='${params.id}' `
var cardata = await query(sql2)
var sql = ` update car set count= `
if(params.data==true){
if(cardata[0].count==1){
return {affectedRows:0}
}
sql+=` count-1 `
}else{
sql+=` count+1 `
}
sql+=` where id=${params.id} `
return await query(sql)
}See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-staticto a specific path
Options
maxageBrowser cache max-age in milliseconds. defaults to 0hiddenAllow transfer of hidden files. defaults to falseindexDefault file name, defaults to 'index.html'deferIf true, serves afterreturn next(), allowing any downstream middleware to respond first.gzipTry to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true.brTry to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists (note, that brotli is only accepted over https). defaults to true.- setHeaders Function to set custom headers on response.
extensionsTry to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults tofalse)
Example
const indexService = require('../service/index');
const redis = require('../extend/redis')
const jwt=require('../extend/help')
const xlsx = require('node-xlsx')
const Logon = async(ctx)=>{
var res1 = await indexService.CheckAccount(ctx.request.body)
if(res1.length>0){
return ctx.body={
code:403,
message:''
}
}
var res2 = await indexService.AddUser(ctx.request.body)
if(res2.affectedRows>0){
return ctx.body={
code:200,
message:''
}
}else{
return ctx.body={
code:403,
message:''
}
}
}
const SendCode = async(ctx)=>{
var phone=ctx.request.body.phone;
if(!phone){
return ctx.body={
code:403,
message:''
}
}
var checkphone=/^1[3-9]\d{9}$/
if(!checkphone.test(phone)){
return ctx.body={
code:403,
message:''
}
}
var res1 = await indexService.CheckAccount(ctx.request.body)
if(res1.length==0){
return ctx.body={
code:403,
message:''
}
}
var rediscode = await redis.get(phone)
if(rediscode == null){
var code = Math.random().toFixed(6).slice(-6)
await redis.setex(phone,60,code);
return ctx.body={
code:200,
message:'',
data:code
}
}else{
return ctx.body={
code:403,
message:''
}
}
}
const Login = async (ctx) => {
var res1 = await indexService.CheckAccount(ctx.request.body)
if (res1.length == 0) {
return ctx.body = {
code: 403,
message: ""
}
}
var rediscode = await redis.get(ctx.request.body.phone)
if (rediscode == ctx.request.body.code) {
var token = await jwt.signtoken(res1[0])
return ctx.body = {
code: 200,
message: "",
data: 'Bearer ' + token
}
} else {
return ctx.body = {
code: 403,
message: ""
}
}
}
const AddCar=async(ctx)=>{
ctx.request.body.userid=ctx.state.user.data.id
var res = await indexService.AddCar(ctx.request.body)
if(res.affectedRows>0){
return ctx.body={
code:200,
message:''
}
}else{
return ctx.body={
code:403,
message:''
}
}
}
const DeleteCar = async (ctx) => {
var res = await indexService.DeleteCar(ctx.request.query)
if (res.affectedRows > 0) {
return ctx.body = {
code: 200,
message: ""
}
} else {
return ctx.body = {
code: 403,
message: ""
}
}
}
const CarShow=async(ctx)=>{
ctx.request.query.userid = ctx.state.user.data.id
var res = await indexService.CarShow(ctx.request.query)
if(res.cardata.length>0){
return ctx.body={
code:200,
message:'',
data:res
}
}else{
return ctx.body={
code:403,
menubar:''
}
}
}
const UpdateCar = async(ctx)=>{
var res = await indexService.UpdateCar(ctx.request.body)
if(res.affectedRows>0){
return ctx.body={
code:200,
message:''
}
}else{
return ctx.body={
code:403,
message:''
}
}
}See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
koajs/mount Mount
koa-staticto a specific pathCCCCCCAAAAARRRR
<body>
<div>
<span> </span><input type="text" id="shopname">
<input type="button" value=" " onclick="Query()">
</div>
<table>
<thead>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</thead>
<tbody id="data"></tbody>
</table>
<div>
<input type="button" value=" " onclick="Page(true)">
<input type="button" value=" " onclick="Page(false)">
</div>
</body>
<script>
var page = 1;
var size = 3;
var zcount = 0
const CarShow = async () => {
var data = {
page,
size,
shopname: document.getElementById('shopname').value
}
var res = await request.get('/car/show', {
params: data
})
if (res.data.code == 200) {
document.getElementById('data').innerHTML = res.data.data.cardata.map((i) => {
return `
<tr>
<td>${i.shopname}</td>
<td>${i.price}</td>
<td>
<input type="button" value="-" onclick="Count(${i.id},true)">
${i.count}
<input type="button" value="+" onclick="Count(${i.id},false)">
</td>
<td>${i.zprce}</td>
<td> </td>
</tr>
`
}).join('')
zcount = res.data.data.zcount
} else {
document.getElementById('data').innerHTML = ''
}
}
CarShow()
const Page = (data) => {
if (data) {
if (page == 1) {
return alert('')
} else {
page = page - 1
}
} else {
if (page == Math.ceil(zcount / size)) {
return alert('')
} else {
page = page + 1
}
}
CarShow()
}
const Query = () => {
page = 1
CarShow()
}
const Count = async (id, data) => {
var datas = {
id,
data
}
var res = await request.put('/car/update', datas)
if (res.data.code == 200) {
CarShow()
} else {
return alert('')
}
}
</script>See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-staticto a specific path
LOGIN
<body>
<div>
<span> </span><input type="text" id="phone">
<br>
<span> </span><input type="text" id="code">
<br>
<input type="button" value=" " onclick="SendCode()">
<input type="button" value=" " onclick="Login()">
</div>
</body>
<script>
const SendCode=async()=>{
var res = await request.post('/user/sendcode',{phone:document.getElementById('phone').value})
if(res.data.code==200){
return alert(res.data.data)
}else{
return alert(res.data.message)
}
}
const Login=async()=>{
var data={
phone:document.getElementById('phone').value,
code:document.getElementById('code').value,
}
var res = await request.post('/user/login',data);
if(res.data.code==200){
localStorage.setItem('token', res.data.data)
alert(' ')
}
}
</script>See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-staticto a specific path
License
MIT
1.0.0
2 years ago