menu
up-caret
down-caret

ZEIT

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

# Micro

Asynchronous HTTP microservices

Trending conversations
503 on deployment
@codelayer · 22d
BRU1 instances are timing out
@helguita · 31d
Images not showing on CRA deployment
@passingmagic · 4h
$ now --target doing nothing
@spy-seth · 13h
Deploying Node v6
@mkern · 34d

How to run @now/node Lambda functions on localhost

ZEIT/Micro · November 18, 2018 at 5:14am

How to run @now/node Lambda functions on localhost

November 18, 2018 at 5:14am (Edited 4 months ago)

I have now.json like this

{
"version": 2,
"builds": [
{
"src": "package.json",
"use": "@now/static-build"
},
{
"src": "api/*.js",
"use": "@now/node"
}
]
}

Run now:

$ now
> Deploying ~\Desktop\iss\blog-counter under marskid
https://blog-counter-3h06ukiux.now.sh
╶ api/functionA.js Initializing
┌ api/functionA.js Ready [36s]
└── λ api/functionA.js (2.42KB) [iad1]
┌ api/index.js Ready [41s]
└── λ api/index.js (3.11KB) [iad1]
┌ api/functionB.js Ready [36s]
└── λ api/functionB.js (2.42KB) [iad1]
┌ package.json Ready [1m]
├── css/app.f8b1e2ac.css (428B)
├── favicon.ico (1.12KB)
├── img/logo.82b9c7a5.png (6.69KB)
├── index.html (778B)
├── js/about.8b907bfe.js (484B)
├── js/about.8b907bfe.js.map (1.46KB)
├── js/app.0aa828d9.js (6.09KB)
├── js/app.0aa828d9.js.map (30.55KB)
├── js/chunk-vendors.985c652d.js (112.04KB)
└── js/chunk-vendors.985c652d.js.map (533.87KB)
> Success! Deployment ready [1m]

The Vue SPA on root of the project. The api contains Lambda functions like `functionA.js` and `functionB.js`.

node_modules
package.json
yarn.lock
index.js
functionA.js
functionB.js

I can use micro run index.js on localhost and proxy localhost:3000 to Vue project.

index.js:

module.exports = (req, res) => {
res.end('Welcome from index.js')
}

functionA.js:

module.exports = (req, res) => {
res.end('Welcome from functionA.js')
}

package.json:

{
"name": "blog-counter-apis",
"version": "1.0.0",
"description": "Blog counter backend",
"main": "index.js",
"scripts": {
"start": "micro",
"dev": "micro-dev"
},
"author": "Melvin Wang",
"license": "MIT",
"private": false,
"dependencies": {
"micro": "^9.3.3",
"micro-cors": "^0.1.1"
},
"devDependencies": {
"micro-dev": "^3.0.0"
}
}

vue.config.js:

module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: {
'^/api': ''
}
}
}
}
}

I can only access localhost:8080/api and get response "Welcome from index.js". I want functionA.js and functionB.js works like Lambda functions on localhost development:

http://localhost:8080 is the main page.

http://localhost:8080/api/index.js return Welcome from index.js

http://localhost:8080/api/functionA.js return Welcome from functionA.js

http://localhost:8080/api/functionB.js return Welcome from functionB.js

Any suggestion will helpful.


November 18, 2018 at 5:57am

We are currently working on this functionality. We plan to expose it as now dev. Stay tuned to news here and on Twitter

like-fill
32
  • reply
  • like

November 19, 2018 at 6:28pm

I had the same question. Ended up creating a local Express app that mounted the microservices for local development. I used Create React App to create my frontend and it can proxy to a backend on another port (I assume this is a Webpack Dev-server capability, but I'm not sure) Have a look at https://github.com/Vinnovera/now_monorepo_react if you wish. Not sure it's a sustainable solution yet, but it worked.

  • reply
  • like

November 20, 2018 at 2:12am

I'm also using vue-cli and have been using this for a dev server:

const http = require('http');
const routes = {
'/api': require('../api/src/index'),
'/api/functionA': require('../api/src/functionA'),
'/api/functionB': require('../api/src/functionB'),
};
http
.createServer(async (req, res) => {
try {
await routes[req.url](req, res);
} catch (err) {
res.writeHead(404);
res.end();
}
})
.listen(process.env.PORT || 1337);

With a a vue.config.js:

devServer: {
proxy: {
'/api': {
target: 'http://localhost:1337',
},
},
}
Edited
like-fill
2
  • reply
  • like

December 4, 2018 at 9:11am

For the time being I created a module that reads your now.json and serves whatever you have in routes field https://www.npmjs.com/package/now-lambda-runner

like-fill
2
  • reply
  • like

December 9, 2018 at 6:17am

December 18, 2018 at 5:17pm

Not sure I follow... isn't AWS Lambda API completely different than Now Lambda? I've been dreading this since I read the "now 2.0" launch stuff but a cursory reading looked to me like the now lambda API is pretty much like micro in terms of req, res... Is it not?

  • reply
  • like

For the time being I created a module that reads your now.json and serves whatever you have in routes field https://www.npmjs.com/package/now-lambda-runner

Nice! Actually kind of funny using express to emulate what now lambdas which are more similar to micro from my POV... maybe a micro version using micro-route or similar down the road. Funny = I moved a few of my express servers to micro/now and it's kind of a pain to move them to now lambda and use micro as dev env.

  • reply
  • like

@krasimir thanks a lot for publishing that!

  • reply
  • like

Will make sure now dev learns from your project as well.

like-fill
1
  • reply
  • like

@rwboyer sorry for that, it was misleading, I am using now-lambda-runner with nodemon. it is very smoothe expierence develpoing this way

  • reply
  • like

December 21, 2018 at 6:12am

Nice! Actually kind of funny using express to emulate what now lambdas which are more similar to micro from my POV... maybe a micro version using micro-route or similar down the road. Funny = I moved a few of my express servers to micro/now and it's kind of a pain to move them to now lambda and use micro as dev env.

That's a really good point. I'll have to educate myself on how micro works and will probably port the module to use that instead of Express.

  • reply
  • like

December 21, 2018 at 2:39pm

That's a really good point. I'll have to educate myself on how micro works and will probably port the module to use that instead of Express.

A few specifics for my hacked up but workable dev environment right now, I use micro + fs-router this combo allows me to put all of my "lambdas" in one folder and emulate the default behavior of now v2. I then only spec builds for the files under that routes folder (but I do not exclude other files that are "required" so they are available for the build, I just do not spec a build in now.json. Last I just rewrite the routes via now.json to get rid of the "routes" path used by fs-router for the API. This allows me to have a dev environment that is REALLY close and very convenient that I can push to now v2 with zero differences (just make sure you stay close to the vanilla node http) even most of the micro nice stuff like send/body parsers/etc work fine in both environments. As an added bonus the whole thing will deploy anywhere and work fine as a monolith (like express) IE now v1, heroku, whatever...

  • reply
  • like

@rwboyer that looks good. Actually my next update will be to get rid of express and replace it with node's http server. What I'm hoping to is to just use the router of micro.

  • reply
  • like

@rwboyer that looks good. Actually my next update will be to get rid of express and replace it with node's http server. What I'm hoping to is to just use the router of micro.

micro doesn't have a router you have to add one like fs-router (there are others that emulate express exactly but I like fs-router because it's very light and behaves a lot like the @now/node does in V2 (because micro is pretty similar to node http/identical) the cools stuff like send and the body parsers are real time savers and are very lightweight.

like-fill
1
  • reply
  • like

@rwboyer that looks good. Actually my next update will be to get rid of express and replace it with node's http server. What I'm hoping to is to just use the router of micro.

Ps. just to be clear in the scenario I laid out with micro the fs-router does nothing when deployed to now v2 in fact it's not included in the builds at all... it's only purpose is to emulate the now v2 lambda routing in dev. When deployed I DO USE a lot of the micro stuff (body parsers, send (convenience function), micro-cors, micro-jwt-auth...

Edited
  • reply
  • like

module.exports = cors(async function (req, res){

  • reply
  • like

above is typical lambda entry point. Make note this will run fine under micro + fs-router as well as @now/node but you can't use the post type routing that fs-router provides (IE module exports whatever.POST ) you have to discriminate post type inside the lambda/fucntion (just like plain micro) or with route specs in now.json

  • reply
  • like

December 25, 2018 at 9:26pm

Just released a tool to help locally testing Now Node.js based lambdas. It uses micro underneath, and avoids shared scope between executions throw module cache cleaning. Hope it helps someone: https://github.com/lucasconstantino/now-we-test

like-fill
5
  • reply
  • like

December 27, 2018 at 8:28am

Just released a tool to help locally testing Now Node.js based lambdas. It uses micro underneath, and avoids shared scope between executions throw module cache cleaning. Hope it helps someone: https://github.com/lucasconstantino/now-we-test

Thanks! It worked well! Is there any ideas how to plug nodemon or something like that?

  • reply
  • like

December 27, 2018 at 8:22pm

Thanks! It worked well! Is there any ideas how to plug nodemon or something like that?

I may be doing it the prehistoric way but... I just use node to fire up micro or whatever.... as in "node --inspect node_modules/micro-dev/bin/micro-dev.js" personally the setup I use is like a hammer but works for now while were waiting for something slick ;-) = micro + fs-router with the actual routes for production mapped in now.json (not that you have to do that if you don't use any of fs-routers slick parameter decoration/decode in the file names, in fact if you don't you pretty much have the same thing as @lucas-constantino did as the default routing for fs-router is what now/node does). It serves me well as the same code runs on micro in nowV1 nowV2/node-server and nowV2/node builders as well as in dev, heroku, etc...

  • reply
  • like

Thanks! It worked well! Is there any ideas how to plug nodemon or something like that?

I guess you are talking about the serving server, right? If that's so, it is unlikely that you'll need nodemon - the lambdas have their module cache erased before each run to avoid conflicting contexts and scopes between each execution, so new code should be available on subsequent calls already!

  • reply
  • like