GithubHelp home page GithubHelp logo

Comments (8)

icebob avatar icebob commented on June 7, 2024 1

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.

icebob avatar icebob commented on June 7, 2024 1

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.

icebob avatar icebob commented on June 7, 2024

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.

davicente avatar davicente commented on June 7, 2024

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.

davicente avatar davicente commented on June 7, 2024

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.

icebob avatar icebob commented on June 7, 2024

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.

davicente avatar davicente commented on June 7, 2024

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.

davicente avatar davicente commented on June 7, 2024

Great.
That makes sense.
Thanks a lot for your help.
I close the issue.

from moleculer-web.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.