Comments (8)
I think there is some base problem. If you can start your service file with node services\myservice.js
it's wrong. A service should be similar as this:
const { MoleculerError } = require("../src/errors");
module.exports = {
name: "math",
actions: {
add(ctx) {
return Number(ctx.params.a) + Number(ctx.params.b);
},
sub(ctx) {
return Number(ctx.params.a) - Number(ctx.params.b);
},
mult: {
params: {
a: "number",
b: "number"
},
handler(ctx) {
return Number(ctx.params.a) * Number(ctx.params.b);
}
},
div: {
params: {
a: { type: "number", convert: true },
b: { type: "number", notEqual: 0, convert: true }
},
handler(ctx) {
let a = Number(ctx.params.a);
let b = Number(ctx.params.b);
if (b != 0 && !Number.isNaN(b))
return a / b;
else
throw new MoleculerError("Divide by zero!", 422, null, ctx.params);
}
}
}
};
from moleculer-web.
Here is a simple example:
// services/www.service.js
"use strict";
const express = require("express");
const ApiGateway = require("moleculer-web");
module.exports = {
name: "www",
mixins: [ApiGateway],
settings: {
port: process.env.PORT || 3000,
},
created() {
const app = express();
app.use(this.express());
this.app = app;
},
started() {
this.app.listen(Number(this.settings.port), err => {
if (err)
return this.broker.fatal(err);
this.logger.info(`WWW server started on port ${this.settings.port}`);
});
},
stopped() {
if (this.app.listening) {
this.app.close(err => {
if (err)
return this.logger.error("WWW server close error!", err);
this.logger.info("WWW server stopped!");
});
}
}
};
from moleculer-web.
I tried your example and it works, no ServiceSchemaError
.
However, it throws ServiceNotFoundError
if link is opened because there is no test
service.
from moleculer-web.
Thanks for your quick reply and for testing it. I tried it creating again a new project from the scratch, but I got the same error.
I removed test service on porpose in order to simplify the example, and I suppose it should at least start without errors.
Can you review my package.json in case you see anything wrong on it?
{
"name": "MyService",
"version": "1.0.0",
"description": "My Moleculer project",
"scripts": {
"dev": "moleculer-runner --repl --hot services",
"start": "moleculer-runner services"
},
"keywords": [
"microservices",
"moleculer"
],
"author": "",
"devDependencies": {
"moleculer-repl": "^0.3.0"
},
"dependencies": {
"express": "^4.16.3",
"moleculer": "^0.13.1",
"moleculer-web": "^0.8.0"
},
"engines": {
"node": ">= 6.x.x"
}
}
I'm using
node v8.11.3
npm v5.6.0
from moleculer-web.
After several tries, I discovered that launching the service with node services\myservice.js
runs well, but with npm run start or npm run dev is when it fails:
"scripts": {
"dev": "moleculer-runner --repl --hot services",
"start": "moleculer-runner services"
},
from moleculer-web.
Your file is a simple Node app which creates broker, loads a service and starts a broker. But these steps should do Moleculer Runner. I recommend you that create a project with moleculer init
and modify the services\api.service.js
to use express
. Like this
from moleculer-web.
The problem with that approach is how I get the service to join it to express framework:
const app = express();
app.use("", svc.express());
I'd need svc object from somewhere, and I don't know how to get with your approach.
Do you see my point?
(thanks again for your support)
from moleculer-web.
Great.
That makes sense.
Thanks a lot for your help.
I close the issue.
from moleculer-web.
Related Issues (20)
- Q&A: How can I remove the service name from the api endpoint? HOT 6
- Could not handle the request when the request come with files and data in multipart HOT 2
- Sometime the list of REST api is missing some functions HOT 1
- multipartHandler throws unhandled error if payload is too large
- CORS Preflight Issue HOT 2
- Response headers affectation HOT 2
- Missing interface when upload file. HOT 2
- resetTime should accept function
- Undefined `actions` property in `/api/~node/services?withActions=true` causes empty REST API page HOT 1
- Route-level `mergeParams` doesn't exist for auto aliases HOT 1
- Cannot skip route with middleware HOT 1
- Improve routes priorization HOT 1
- RateLimit didn't check if trusted proxy send the header x-forwarded-for HOT 4
- REST shortcut doesn't handle action object correctly
- Typescript error: import { IParseOptions } from 'qs'
- Can we get an official interface for ctx.meta.$ fields available in moleculer-web?
- Routes with autoAliases not considered for started HOT 1
- Allow define action type in auto aliases (rest action param) HOT 2
- What is the benefit of using ctx.meta.$location & ctx.meta.$responseType vs just setting via ctx.meta.$responseHeaders? HOT 4
- update request body value in onBeforeCall hook not working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moleculer-web.