londor
v1.0.1
Published
A tiny typescript and express based web framework
Downloads
5
Maintainers
Readme
Londor - Tiny and Neat Express
Londor is a nice little service based framework on top of express. It's 2 core values
- Decrease Abstractions
- Increase Readability
- It doesn't hide anything from express
Getting Started
- You'll need Node 6.10.x or higher
- We highly recommmend using TypeScript 2.5.1 or higher
- Your tsconfig.json file will need:
- to target
es6
- use
commonjs
- set
experimentalDecorators
to true - set
emitDecoratorMetadata
to true
- to target
- Run
npm install londor --save
It should look something like below.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
},
"exclude": [
"node_modules"
]
}
Examples
A simple server
import { Server } from 'londor'
const server = new Server({
port: 4000
})
server.start()
.then(() => {
console.log("The server is started on port 4000!")
})
.catch((err) => {
console.error(err)
})
Adding a service
A service is just a class
that you can add to a Server
instance.
- You can expose methods as http endpoints using
@Get, @Post, @Put, @Delete
etc... - You can read parameters using the
@Param('carId')
or just@Param
decorator - Services don't need http endpoints at all
import { Server, Get, Post, Put, Delete, BaseRoute, Body } from 'londor'
@BaseRoute('/cars')
class CarService {
cars = [{
carId: 1,
name: 'toyota',
}, {
carId: 2,
name: 'ford',
}]
@Get('/all')
getAllCars(){
return this.cars
}
@Post('/')
async addNewCar(@Body body: any) {
this.cars.push(body.car)
return body
}
@Put('/:carId')
async updateCar(@Body body: any) {
this.cars.push(body.car)
return body
}
@Delete('/:carId')
async deleteCar(@Param carId: string) {
for(let car of this.cars) {
if (car.carId === carId) {
this.cars.splice(this.cars.indexOf(car), 1)
}
}
}
}
const server = new Server({
port: 4000
})
server.addService(new CarService())
server.start()
.then(() => {
console.log("The server is started on port 4000!")
})
.catch((err) => {
console.error(err)
})
Using Express Middlewares
The server can easily attach express middlewares. Simply use the familiar use
functionality
import * as cors from 'cors'
server.use(cors())
Express Middlewares in Services
You can add additional middlewares before
import { Get, UseMiddleware } from 'londor'
Serving Static Files
Serving Static Files is super easy with @ServeStatic
- The first parameter is the route of the static file
- The second parameter is the path to file to serve.
This is simply a wrapper around express.static
API
import { ServeStatic, BaseRoute } from 'londor'
@BaseRoute('/dashboard')
@ServeStatic('/', 'path/to/index.html')
@ServeStatic('/admin', 'path/to/admin.html')
class DashboardService {
// .. more implementation goes here
}
server.addService(new DashboardService())
server.start()
.then(() => {
console.log("The server is started on port 4000!")
console.log("The dashboard is at http://localhost:4000/dashboard")
console.log("The admin panel is at http://localhost:4000/dashboard/admin")
})
.catch((err) => {
console.error(err)
})
Contributing
After npm install
- To Build
npm run build
- To Clean Artifacts
npm run clean
- To Test
npm run test
- To Lint
npm run lint
- To Buld and Watch when you make changes
npm run watch
Debugging with Visual Studio Code
- Set a breakpoint in your code ending in
.ts
or your test ending in.spec.ts
- Run Either
Launch Program
orLaunch Tests
in the debug pane. - Run Either
Launch Program
orLaunch Tests
in the debug pane.