menu

ZEIT

Our mission is to make cloud computing as easy and accessible as mobile computing. You can find our Next.js community here.

Channels
Team

How to run Micro on Now V2

December 21, 2018 at 6:11pm

How to run Micro on Now V2

December 21, 2018 at 6:11pm

Ive written a small graphql service using micro (followed the example). It runs fine under micro-dev and micro on my localhost. It deploys and runs under Now V1 but doesn't run under Now V2. Im using the

/node builder which reports a successful.

But i get a 504: An error occurred with your deployment Code: LAMBDA_INVOCATION_TIMEOUT.

From the Logs I get this

Error: socket hang up

at createHangUpError (_http_client.js:331:15)

at Socket.socketOnEnd (_http_client.js:423:23)

at emitNone (events.js:111:20)

at Socket.emit (events.js:208:7)

at endReadableNT (_stream_readable.js:1064:12)

at _combinedTickCallback (internal/process/next_tick.js:138:11)

at process._tickDomainCallback (internal/process/next_tick.js:218:9)

12/21 06:08 PM (1m)

REPORT RequestId: 1a7e1f74-054b-11e9-b9fc-33fedabc0807 Duration: 120102.08 ms Billed Duration: 120200 ms Memory Size: 1024 MB Max Memory Used: 23 MB

12/21 06:08 PM (1m)

RequestId: 1a7e1f74-054b-11e9-b9fc-33fedabc0807 Process exited before completing request


December 21, 2018 at 6:14pm

In now v2 you need to use micro programatically, therefore with a server listen call. Then you should use

/node-server.

Micro does not have something that Koa does, like a callback method: https://github.com/zeit/now-examples/blob/master/nodejs-koa/index.js

We will, however, release

/micro very soon!

  • reply
  • like

A little example of micro + v2:

const { router, get } = require('micro-fork')
const micro = require('micro')
const notFound = (req, res) => micro.send(res, 404, 'Not found route')
const handler = router()(
get('/*', notFound)
)
const server = micro(handler)
server.listen()

Then in now.json:

{
"version": 2,
"name": "sandbox",
"builds": [{ "src": "src/test.js", "use": "@now/node" }],
"routes": [{ "src": "/(.*)", "dest": "/src/test.js" }],
"public": true
}
Edited
like-fill
2
  • reply
  • like

December 27, 2018 at 7:49am

A little example of micro + v2:

const { router, get } = require('micro-fork')
const micro = require('micro')
const notFound = (req, res) => micro.send(res, 404, 'Not found route')
const handler = router()(
get('/*', notFound)
)
const server = micro(handler)
server.listen()

Then in now.json:

{
"version": 2,
"name": "sandbox",
"builds": [{ "src": "src/test.js", "use": "@now/node" }],
"routes": [{ "src": "/(.*)", "dest": "/src/test.js" }],
"public": true
}

is there any detailed example on building Now+Micro workflow?

  • reply
  • like

I want to solve simplest case - having a code with just normal get/post router which works on localhost and can be fast deployed to now.

  • reply
  • like

E.g. if I dont specify port in server.listen() it dont work locally. If i do it shows 500 error with Error: listen EACCES 0.0.0.0:80

  • reply
  • like

Also is there any documentation on micro-fork you used?

  • reply
  • like

And yes, I've heard about now dev, but it is not there yet

  • reply
  • like

I think you need to wait for

/micro.

Also is there any documentation on micro-fork you used?

Yes. But I highly discourage the use of a router in now v2 due to our recommendation to break code as much as possible.

  • reply
  • like

I think you need to wait for

/micro.

Also is there any documentation on micro-fork you used?

Yes. But I highly discourage the use of a router in now v2 due to our recommendation to break code as much as possible.

I would love to follow your recommendations if I would know how to do that:)

  • reply
  • like

assume I use routes in now.json - 1) how do I debug them locally without deployment? 2) How do I implement parts of RESTful approach here? In case one of endpoints wants to recieve POST, other - get etc.

  • reply
  • like

assume I use routes in now.json - 1) how do I debug them locally without deployment? 2) How do I implement parts of RESTful approach here? In case one of endpoints wants to recieve POST, other - get etc.

1) Until now dev is not released, it can be a pain to debug them locally.

2) We have a methods in our routes configuration: https://zeit.co/docs/v2/deployments/configuration/#routes

EG:

{ "src": "/", "methods": ["GET"] , "dest": "/front/index.html" },
{ "src": "/", "methods": ["POST"] ,"dest": "/back/handlers/chop.js" },
Edited
like-fill
1
  • reply
  • like

1) don't know this yet. I have a manual setup which requires all the lambdas and starts a local server with those mounted at the defined routes (read from now.json)

2) as

said. Example: https://github.com/zeusdeux/til.mudit.xyz/blob/master/now.json#L11-L45

  • reply
  • like

1) Until now dev is not released, it can be a pain to debug them locally.

2) We have a methods in our routes configuration: https://zeit.co/docs/v2/deployments/configuration/#routes

EG:

{ "src": "/", "methods": ["GET"] , "dest": "/front/index.html" },
{ "src": "/", "methods": ["POST"] ,"dest": "/back/handlers/chop.js" },

Is it good practice to build 4 destinations in routes for CRUD? Or I should better somehow split them on one endpoint? And after, in case of POST how do I parse body without all this painful req.on('data'...) stuff?

  • reply
  • like

Is it good practice to build 4 destinations in routes for CRUD? Or I should better somehow split them on one endpoint? And after, in case of POST how do I parse body without all this painful req.on('data'...) stuff?

You should ask yourself the first question. It truly depends on how much traffic you expect to get on each function. The bigger the lambda, the worst. Benchmark everything!

req.on('data'...)

?

  • reply
  • like
And after, in case of POST how do I parse body without all this painful req.on('data'...) stuff?
https://www.npmjs.com/package/body
  • reply
  • like

you can use the json function that micro provides. I also use the send function to send responses (easier then the native node http syntax).

  • reply
  • like

I don't/haven't used micro but thanks for the heads up. Also,

is the OP not me ^_^

  • reply
  • like

January 4, 2019 at 1:09am

I've just released a temporary now-micro builder while

/micro official builder is not yet released. Here it goes: https://github.com/lucasconstantino/now-micro

like-fill
9
  • reply
  • like

March 26, 2019 at 7:58am

I've just released a temporary now-micro builder while

/micro official builder is not yet released. Here it goes: https://github.com/lucasconstantino/now-micro

Thanks, the builder solved deploying micro sever issue

  • reply
  • like

May 12, 2019 at 9:57pm

I created a sample app using the apollo-server-micro with typescript: https://github.com/y4nnick/now-graphql-typescript

  • reply
  • like

November 1, 2019 at 8:37pm

What's the advantage of using micro vs a plain express server?

  • reply
  • like
private
This channel has been archived