W
W
Wasya UK2019-05-17 12:32:24
Node.js
Wasya UK, 2019-05-17 12:32:24

How to reach the service on the docker?

I wrote a simple service on the node and launched it using docker (as in the instructions), everything seems to work, but when I went into the postman, I couldn’t get through to the service. How to make a request to such servers?
I get the following result:
5cde7f65c8be4001511707.jpeg
Did I do everything right and what path should I enter in postman? Thanks in advance
dockerfile

FROM node:8

WORKDIR /srv
ADD . .
RUN npm install

EXPOSE 3000
CMD ["node", "server.js"]

rule.json
{
  "ListenerArn": "placeholder",
  "Conditions": [
    {
      "Field": "path-pattern",
      "Values": [
        "/api/tasks*"
      ]
    }
  ],
  "Priority": 3,
  "Actions": [
    {
      "Type": "forward",
      "TargetGroupArn": "placeholder"
    }
  ]
}

server.js
const Koa = require('koa');
const Router = require('koa-router');
const db = require('./db.json');

const app = new Koa();
const router = new Router();

// logger
app.use(async (ctx, next) => {
  await next();
  const rt = ctx.response.get('X-Response-Time');
  console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});

// x-response-time
app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set('X-Response-Time', `${ms}ms`);
});

router.get('/api/tasks/by-forester/:foresterId', async (ctx, next) => {
  const id = parseInt(this.params.foresterId);
  ctx.body = db.posts.filter((task) => task.forester == id);
  await next();
});

router.get('/api/tasks/by-supervisor/:supervisorId', async (ctx, next) => {
  const id = parseInt(this.params.supervisorId);
  ctx.body = db.posts.filter((task) => task.supervisor == id);
  await next();
});

router.get('/api/', async (ctx, next) => {
  ctx.body = "API ready to receive requests";
  await next();
});

router.get('/', async (ctx, next) => {
  ctx.body = "Ready to receive requests";
  await next();
});

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(3000, () => {
  console.log("Server work on port 3000");
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chupasaurus, 2019-05-17
@dmc1989

Recreate container with -p 3000:3000. EXPOSE by itself does not forward ports.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question