- Full Web Server (ExpressJS)
- HTTP Library (Axios)
- In-Memory Caching
- SQL-Like JSON Database
- Crypto UUID Generator
- Natural Language Date Parsing
- Natural Language Timestamps
- Filesystem Read/Write
- Filesystem Prepend/Append
- Natural Language Cron
- Run SHELL from NodeJS
npm install fwd/server
const server = require('@fwd/server')
server.get('/', (req, res) => {
res.send("Hello, World!")
})
server.start(8080)
Static files are served from /public by default. Change with
server.start(8080, { public: '/dist' })
.
server.get('/', async (req, res) => {
var settings = await server.database.get('settings')
res.render('index', { config: settings, query: req.query }) // /views/index.ejs
})
server.post('/register', async (req, res) => {
var user = await server.database.create('users', {
id: server.uuid(),
name: req.body.name,
created_at: server.timestamp()
})
// fetching it again just to show off API
user = await server.database.findOne('users', { name: req.body.name })
res.send({ user })
})
server.get('/joke', async (req, res) => {
var joke = (await server.http.get('https://api.chucknorris.io/jokes/random')).data
res.send({ joke })
})
server.get('/user/:id', async (req, res) => {
var user = await server.database.findOne('users', { id: req.params.id })
if (!user) return res.send({ error: 401 })
res.send({ user })
})
server.start(8080, {
timezone: 'America/New_York' // optional, just showing it off
})
server.get('/', (req, res) => {
res.send("Hello, World!")
})
server.use((req, res, next) => {
// do stuff
console.log( req.ip, req.originalUrl )
next()
})
server.start(8080)
Includes CORS and EJS rendering for your convenience.
;(async () => {
var joke = await server.http.get('https://api.chucknorris.io/jokes/random')
console.log( joke.data )
})()
const database = server.database
;(async () => {
await database.get('users') // list all users
await database.create('users', { name: john }) // creates user, id & created_at will be generated if not provided
await database.find('users', { name: 'Joe' }) // Filter users by query
await database.findOne('users', { id: 1 }) // find user with id of 1
await database.update('users', 1, { name: "John" }) // update user with id of 1
await database.remove('users', 1) // remove user with id of 1
})()
Dedicated Github: @fwd/database
server.cache('unique_key', { fname: 'Joe' })
console.log( server.cache('unique_key') ) // { fname: 'Joe' }
Added September 7th, 2021
console.log( server.date('next friday') )
// Fri Sep 12 2014 12:00:00 GMT-0500 (CDT)
More info: @fwd/time
server.time(5, 'seconds') // 5000
server.time(1, 'minute') // 60000
// example
setInterval(() => {
console.log("The time is:", server.timestamp('LLLL'))
}, server.time(24, 'minutes'))
server.timestamp() // UNIX timestamp
server.timestamp('LL') // September 28, 1994
server.timestamp('LLL') // September 28, 1994 4:30PM
server.timestamp('LLL', 'America/New_York') // Optional, timezones.
More info: @fwd/cache
server.cron(() => {
console.log("The time is:", server.timestamp('LLLL'))
}, 'every 1 hour')
More info: @fwd/cron
const server = require('@fwd/server')
server.uuid() // 9e471b08-38fe-11eb-adc1-0242ac120002
server.uuid(true) // short uuid, 9e471b08
;(async () => {
var cpu_usage = await server.exec(`awk '/cpu /{print 100*($2+$4)/($2+$4+$5)}' /proc/stat`)
console.log( cpu_usage )
})()
;(async () => {
var data = await server.read('./grades.csv')
console.log( data )
})()
;(async () => {
var data = await server.write('./notes.txt', 'John Doe')
console.log( data )
})()
;(async () => {
var data = await server.append('./names.txt', 'John Doe')
console.log( data )
})()
;(async () => {
var data = await server.prepend('./names.txt', 'Joe Mama')
console.log( data )
})()
console.log(server.moment().format('LLLL'))
console.log(server.moment().fromNow())
Give a βοΈ if this project helped you!
Contributions, issues and feature requests are welcome! Feel free to check issues page.
MIT License
Copyright Β© @nano2dev.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.