Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'moleculer-web' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
"use strict";
const _ = require("lodash");
const ApiGateway = require("moleculer-web");
const { UnAuthorizedError } = ApiGateway.Errors;
module.exports = {
name: "api",
mixins: [ApiGateway],
settings: {
port: process.env.PORT || 3000,
routes: [{
path: "/api",
authorization: true,
aliases: {
// Login
"POST /users/login": "users.login",
created() {
const route = _.defaultsDeep(mixinOptions.routeOptions, {
use: [
ApiGateway.serveStatic(SwaggerUI.absolutePath())
],
aliases: {
"GET /openapi.json"(req, res) {
// Send back the generated schema
if (shouldUpdateSchema || !schema) {
// Create new server & regenerate GraphQL schema
this.logger.info("♻ Regenerate OpenAPI/Swagger schema...");
try {
schema = this.generateOpenAPISchema();
shouldUpdateSchema = false;
this.logger.debug(schema);
/**
* Static routes
*/
{
path: "/",
use: [
// handle fallback for HTML5 history API
require("connect-history-api-fallback")(),
// Webpack middlewares
...initWebpackMiddlewares(),
// Serve static
ApiGateway.serveStatic("./static"),
],
// Action aliases
aliases: {
},
mappingPolicy: "restrict",
},
]
},
methods: {
/**
* Authenticate from request
*
* @param {Context} ctx
"use strict";
const { ForbiddenError } = require("moleculer-web").Errors;
const DbService = require("moleculer-db");
module.exports = {
name: "comments",
mixins: [DbService],
adapter: new DbService.MemoryAdapter({ filename: "./data/comments.db" }),
/**
* Default settings
*/
settings: {
fields: ["_id", "author", "article", "body", "createdAt", "updatedAt"],
populates: {
"author": {
action: "users.get",
params: {
"use strict";
const { MoleculerClientError } = require("moleculer").Errors;
const { ForbiddenError } = require("moleculer-web").Errors;
const _ = require("lodash");
const slug = require("slug");
const DbService = require("moleculer-db");
module.exports = {
name: "articles",
mixins: [DbService],
adapter: new DbService.MemoryAdapter({ filename: "./data/articles.db" }),
/**
* Default settings
*/
settings: {
fields: ["_id", "title", "slug", "description", "body", "tagList", "createdAt", "updatedAt", "favorited", "favoritesCount", "author", "comments"],
.then(entity => {
if (!entity)
return this.Promise.reject(new MoleculerClientError("Article not found!", 404));
if (entity.author !== ctx.meta.user._id)
return this.Promise.reject(new ForbiddenError());
return this.adapter.removeById(entity._id)
.then(() => ctx.call("favorites.removeByArticle", { article: entity._id }));
});
}
.then(comment => {
if (comment.author !== ctx.meta.user._id)
return this.Promise.reject(new ForbiddenError());
return this.adapter.removeById(ctx.params.id);
});
}