在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:raveljs/ravel开源软件地址:https://github.com/raveljs/ravel开源编程语言:JavaScript 96.6%开源软件介绍:Ravel
Ravel is a tiny, sometimes-opinionated foundation for creating organized, maintainable, and scalable web applications in node.js with ES2016/2017. Note: The Table of ContentsIntroductionRavel is inspired by the simplicity of koa and express, but aims to provide a pre-baked, well-tested and highly modular solution for creating enterprise web applications by providing:
And a few other features, plucked from popular back-end frameworks:
Ravel is deliberately designed to minimize unnecessary dependencies and have a small, well-documented codebase, making it easier to create secure and robust applications you and your users can trust. Ravel is layered on top of, and designed to be used with, awesome technologies, including: Installation
$ npm install ravel ArchitectureRavel applications consist of a few basic parts:
If you're doing it right, your applications will consist largely of Modules (and Errors)
For more information about modules/cities.js const Ravel = require('ravel');
const Error = Ravel.Error;
const Module = Ravel.Module;
const inject = Ravel.inject;
/**
* First, we'll define an Error we will throw when a requested
* city is not found. This Error will be associated with the
* HTTP error code 404.
*/
class MissingCityError extends Error {
constructor (name) {
super(`City ${name} does not exist.`, Ravel.httpCodes.NOT_FOUND);
}
}
/**
* Our main Module, defining logic for working with Cities
*/
@inject('moment', '$log')
@Module('cities')
class Cities {
constructor (moment, $log) {
this.moment = moment;
this.$log = $log
this.cities = ['Toronto', 'New York', 'Chicago']; // our fake 'database'
}
getAllCities () {
return Promise.resolve(this.cities);
}
getCity (name) {
return new Promise((resolve, reject) => {
const index = this.cities.indexOf(name);
if (index !== -1) {
resolve(this.cities[index]);
} else {
// Ravel will automatically respond with the appropriate HTTP status code!
this.$log.warn(`User requested unknown city ${name}`);
reject(new MissingCityError(name));
}
});
}
}
// Export Module class
module.exports = Cities; Middleware
modules/cities.js const Ravel = require('ravel');
const Module = Ravel.Module;
const middleware = Module.middleware;
class MyMiddleware {
// this middleware will be available by name elsewhere in the application
@middleware('custom-middleware')
async doSomething(ctx, next) {
// ... do something before the next middleware runs
await next();
// ... do something after the next middlware runs
}
// this middleware is also available elsewhere by name,
// but is a factory that can receive two arguments
@middleware('another-middleware', { factory: true })
anotherMiddlewareFactory (arg1, arg2) {
return async (ctx, next) {
await next();
}
}
} Routes
For more information about routes/index.js const Ravel = require('ravel');
const Routes = Ravel.Routes;
const inject = Ravel.inject;
const before = Routes.before; // decorator to chain middleware before an endpoint
const mapping = Routes.mapping; // decorator to associate a handler method with an endpoint
@Routes('/') // base path for all routes in this class. Will be prepended to the @mapping.
class ExampleRoutes {
// bind this method to an endpoint and verb with @mapping. This one will become GET /app
@mapping(Routes.GET, 'app')
@before('custom-middleware') // use @before to place multiple middleware (comma-separated names) before appHandler - these could be npm modules, functions on this scope, or defined via @middleware
async appHandler (ctx) {
// ctx is just a koa context! Have a look at the koa docs to see what methods and properties are available.
ctx.body = '<!DOCTYPE html><html><body>Hello World!</body></html>';
ctx.status = 200;
}
@mapping(Routes.GET, 'log')
@before('another-middleware', 1, 2, 'custom-middleware') // use @before to instantiate middleware which accepts arguments, alongside middleware which doesn't.
async logHandler (ctx) {
// ...
}
}
// Export Routes class
module.exports = ExampleRoutes; ResourcesWhat might be referred to as a controller in other frameworks, a For more information about resources/city.js // Resources support dependency injection too!
// Notice that we have injected our cities Module by name.
const Ravel = require('ravel');
const Resource = Ravel.Resource;
const inject = Ravel.inject;
const before = Resource.before; // decorator to add middleware to an endpoint within the Resource
// using @before at the class level decorates all endpoint methods with middleware
@inject('cities')
@Resource('/cities') // base path for all routes in this Resource
class CitiesResource {
constructor (cities) {
this.cities = cities;
}
// no need to use @mapping here. Routes methods are automatically mapped using their names.
async getAll (ctx) { // just like in Routes, ctx is a koa context.
ctx.body = await this.cities.getAllCities();
}
@before('custom-middleware') // using @before at the method level decorates this method with middleware
@before('another-middleware', 1, 2) // use @before multiple times for clarity, if desired
async get (ctx) { // get routes automatically receive an endpoint of /cities/:id (in this case).
ctx.body = await this.cities.getCity(ctx.params.id);
}
// post, put, putAll, delete and deleteAll are
// also supported. Not specifying them for
// this resource will result in calls using
// those verbs returning HTTP 501 NOT IMPLEMENTED
// postAll is not supported, because it makes no sense
}
// Export Resource class
module.exports = CitiesResource; Bringing it all togetherapp.js const app = new require('ravel')();
// parameters like this can be supplied via a .ravelrc.json file
app.set('keygrip keys', ['mysecret', 'anothersecret']);
app.scan('./modules'); //import all Modules from a directory
app.scan('./resources'); //import all Resources from a directory
app.scan('./routes/index.js'); //import all Routes from a file
// start it up!
app.start(); Decorator TranspilationSince decorators are not yet available in Node, you will need to use a transpiler to convert them into ES2016-compliant code. We have chosen Babel as our recommended transpiler. $ npm install @babel/[email protected] @babel/[email protected] [email protected] gulpfile.js const babelConfig = {
'plugins': [['@babel/plugin-proposal-decorators', { 'legacy': true }]]
};
gulp.task('transpile', function () {
return gulp.src('src/**/*.js') // point it at your source directory, containing Modules, Resources and Routes
.pipe(plugins.babel(babelConfig))
.pipe(gulp.dest('dist')); // your transpiled Ravel app will appear here!
}); Running the Application$ node dist/app.js API Documentation |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论