# levity-server

> Simple Nodejs Server

Latest version **0.4.5** (published 2021-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install levity-server
pnpm add levity-server
yarn add levity-server
bun add levity-server
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.5 |
| Published | 2021-11-02 |
| First published | 2020-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ahmed Alaa |
| Maintainers | ahmedalaa23 |
| Keywords | server, restful, API |

## Links

- npm: https://www.npmjs.com/package/levity-server
- Repository: https://github.com/AhmedAlaa23/levity-node-server
- Homepage: https://github.com/AhmedAlaa23/levity-node-server#readme
- Issues: https://github.com/AhmedAlaa23/levity-node-server/issues
- npm.io page: https://npm.io/package/levity-server

## Dependencies (2)

- [raw-body](https://npm.io/package/raw-body.md) ^2.4.1
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.27

## Recent versions

- 0.4.5 (latest) — 2021-11-02
- 0.4.4 — 2021-08-23
- 0.4.3 — 2021-08-12
- 0.4.2 — 2021-07-19
- 0.4.1 — 2021-07-19
- 0.4.0 — 2021-07-17
- 0.3.5 — 2021-07-14
- 0.3.4 — 2021-05-01
- 0.3.3 — 2021-05-01
- 0.3.2 — 2021-04-30
- 0.3.1 — 2020-07-30
- 0.3.0 — 2020-07-30
- 0.2.1 — 2020-07-11
- 0.2.0 — 2020-06-18
- 0.1.0 — 2020-06-17
- … 1 more at https://npm.io/package/levity-server/versions

## README

# Levity Node Server

## Simple Light Node Server

## Installation

```
$ npm install levity-server
```

## Getting Started

```javascript
import application from 'levity-server';

var app = application();

const app = application();

app.listen(3000, ()=>{
	console.log('Server Listening on 3000');
});

app.setRouter({
	nodes: {
		'/users': {
			paths:{
				'/': {
					methods: {
						'GET': {handler: searchUsers},
						'POST': {handler: addUser},
					},
				},
				'/{id}': {
					methods: {
						"GET": {handler: getUser},
						"PUT": {handler: editUser},
						"DELETE": {handler: deleteUser},
					}
				},
				'/{id}/{email}': {
					methods: {
						"GET": {handler: getUserByIdAndEmail},
					}
				},
			},
		},
		
		'/products': {
			paths:{
				'/': {
					methods: {
						'GET': {handler: searchProducts},
						'POST': {handler: addProduct},
					},
				},
				'/{id}': {
					methods: {
						"GET": {handler: getProduct},
						"PUT": {handler: editProduct},
						"DELETE": {handler: deleteProduct},
					}
				},
			},
		},

		'/reports': {
			paths:{
				'/users': {
					methods: {
						'GET': {handler: getUsersReport},
					}
				},
				'/products': {
					methods: {
						'GET': {handler: getProductReport},
					}
				}
			}
		},

		'/get_html_page': {
			paths:{
				'/users': {
					methods: {
						"GET": {handler: getUserHTMLPage},
					}
				}
			}
		},

	},
	
	defaultAuthData: {type:'public', anyInfo: 'defaultAuthInfo'},

	errHandler: errHandler,
	notFoundHandler: notFoundHandler
});

app.auth({handler: authHandler});

app.setAccessControl({ AccessControlAllowOrigin: '*', AccessControlAllowMethods: ['GET','POST','PUT','DELETE','OPTIONS'] });
app.CORSPreFlight();

function authHandler({req, res, routePath, routeAuth}){
	console.log(routePath, routeAuth);
	
	if(routeAuth.type==='public'){
		return true;
	}
	else{
		const bearerToken = req.headers.authorization.split(' ')[1];
		if(bearerToken!=='secret_bearer_token'){
			res.errJson({msg: 'Unauthorized !'}, {status: 401})
			return false;
		}
		return true;
	}
}

function errHandler({err, routePath, res}){
	console.log("err: ", err);
	return res.errJson({status: 'err', 'err': err});
}

function notFoundHandler({reqPath, res}){
	console.log("Not Found: ", reqPath);
	return res.respond({status: 404, contentType: 'application/json', body: JSON.stringify({status: 404, msg: 'Not Found'}) });
}

const searchUsers = (req,res)=>{
	console.log(req.query);
	res.json({msg: "Search Users"})
}
const addUser = (req,res)=>{
	console.log('User:', req.body.name);
	res.json({msg: "Add Users"})
}
const getUser = (req,res)=>{
	return res.json({msg: `Get Users id: ${req.params.id}`})
}
const getUserByIdAndEmail = (req,res)=>{
	return res.json({msg: `Get Users id: ${req.params.id}, email: ${req.params.email}`})
}
const editUser = (req,res)=>{
	console.log(req.body);
	return res.json({msg: `Edit Users ${req.params.id}`})
}
const deleteUser = (req,res)=>{
	return res.json({msg: `Delete Users ${req.params.id}`})
}

const getUserHTMLPage = (req,res)=>{
	return res.html(`<h1>Hello User ${req.query.name}</h1>`)
}
```

---
_Source: https://npm.io/package/levity-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
