Skip to content
/ server Public

Full-Stack NodeJS Toolkit

Notifications You must be signed in to change notification settings

fwd/server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cover

Features

Install

npm install fwd/server

Basic Usage

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' }).

Advanced Usage

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
}) 

HTTP Middleware

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.

Built-in HTTP Client (Axios)

;(async () => {
	var joke = await server.http.get('https://api.chucknorris.io/jokes/random')
	console.log( joke.data )
})()

Built-in Database

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

In-Memory Caching

server.cache('unique_key', { fname: 'Joe' })

console.log( server.cache('unique_key') ) // { fname: 'Joe' }

Natural Language Date Parsing

Added September 7th, 2021

console.log( server.date('next friday') )
// Fri Sep 12 2014 12:00:00 GMT-0500 (CDT)

More info: @fwd/time

Natural Language Timestamps

server.time(5, 'seconds') // 5000
server.time(1, 'minute') // 60000

// example
setInterval(() => {
	console.log("The time is:", server.timestamp('LLLL'))
}, server.time(24, 'minutes'))

Simple Timestamps

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

Natural Language Cron

server.cron(() => {
	console.log("The time is:", server.timestamp('LLLL'))
}, 'every 1 hour')

More info: @fwd/cron

Generate Crypto UUID

const server = require('@fwd/server')

server.uuid() // 9e471b08-38fe-11eb-adc1-0242ac120002 
server.uuid(true) // short uuid, 9e471b08

Execute Shell from NodeJS

;(async () => {
	var cpu_usage = await server.exec(`awk '/cpu /{print 100*($2+$4)/($2+$4+$5)}' /proc/stat`)
	console.log( cpu_usage )
})()

Built-in File Read/Write

;(async () => {
	var data = await server.read('./grades.csv')
	console.log( data )
})()

Write File

;(async () => {
	
	var data = await server.write('./notes.txt', 'John Doe')

	console.log( data )

})()

Append a File

;(async () => {
	var data = await server.append('./names.txt', 'John Doe')
	console.log( data )
})()

Prepend a File

;(async () => {
	var data = await server.prepend('./names.txt', 'Joe Mama')
	console.log( data )
})()

MomentJS Included

console.log(server.moment().format('LLLL'))
console.log(server.moment().fromNow())

🀝 Contributing

Give a ⭐️ if this project helped you!

Contributions, issues and feature requests are welcome! Feel free to check issues page.

πŸ“ License

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.

Stargazers

Stargazers over time