0
\$\begingroup\$

This is a backend side of Node.js running a simple Http endpoints for a monitring system for a time-related task

I recently developed a habit that I would rather spend time to make the code as less as possible because I consider neat looking code is art. I'm lazy to come up with a name for variables and functions, for example, I use alphabet as variable or parameters, and also if any part of the code that duplicate, I'd put them into functions or variables

main.js

const lib=require('./lib')
const express=require("express")
const app=express()

const multer=require('multer')
const upload=multer()

const bodyParser = require("body-parser")
app.use(bodyParser.json())

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
  res.header('Access-Control-Allow-Methods','*')
  next()
})

const veri=require('./veri')

app.get('/veri',veri.run.show)
app.post('/veri',upload.fields([]),async(req,res)=>{
    await veri.run.update(req.body.a)
    res.send('ok')
})

const clock=require('./clock')

app.get('/clock',clock.run.show)
app.post('/clock',upload.fields([]),clock.run.update)
app.get('/sche',clock.run.sche)

app.listen(3001)

clock.js

const lib=require('./lib')
const path='json/clock.json'

const init=async()=>{
    const a=await lib.bb(path)
    a.forEach((x,i)=>{
        if(x==1) a[i]=0
    })
    lib.json2txt(path,a)
}

//init()

const show=async(req,res)=>{
    const a=await lib.bb(path)
    res.send(a)
}

//show('',{send:console.log})

const update=async(req,res)=>{
    const a=await lib.bb(path)
    a[req.body.a]=1
    lib.json2txt(path,a)
    res.send('Done')
}

const sche=async(req,res)=>{
    const a=await lib.bb(path)
    a[req.query.a]=2
    lib.json2txt(path,a)
    init()
    res.send('Done')
}

const mock={
    req:{
        body:{
            a:3
        },
        query:{
            a:0
        }
    }
}

//update(mock.req,{send:console.log})
//sche(mock.req,{send:console.log})

lib.wrapcron('init','55 23 * * 0,1-5',init)

exports.run={update,show,sche}

veri.js

const lib=require('./lib')
const path='json/veri.json'

const update=async k=>{
    const a=await lib.bb(path)
    a[k]=+new Date
    lib.json2txt(path,a)
}

const show=async(req,res)=>{
    const b=await lib.bb(path)
    res.send(b)
}

//show('',{send:console.log})
//update('soAg')

exports.run={update,show}

lib.js

const fs=require('fs')

exports.t=msg=>console.log(`[${new Date().toLocaleString()}] ${msg}`)

exports.json2txt=(fullPath,json)=>{
  fs.writeFile(fullPath,JSON.stringify(json),err=>{
    if(err) exports.t(err)
    else exports.t(`wrote to ${fullPath}`)
  })
}

const txt2json=(fullpath,callback)=>{
  fs.readFile(fullpath,'utf-8',(err,res)=>{
    if(err) exports.t(err)
    else callback(JSON.parse(res))
  })
}

const CronJob = require('cron').CronJob
console.t=msg=>console.log(`${new Date().toLocaleString()} ${msg}`)

exports.wrapcron=(name,cron,f)=>{
  const job = new CronJob(cron, ()=>{
    console.t(`Job ${name} is running`)
    f()
  }, null, true, 'America/Los_Angeles')
  job.start()
  console.t(`Crontab ${name} scheduled: ${cron}`)
}

exports.bb=path=>new Promise((resolve,reject)=>{
  txt2json(path,resolve)
})
\$\endgroup\$
1
  • 1
    \$\begingroup\$ The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    May 3 at 6:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.