webapp

Documentation of Meteor's webapp package.

The webapp package is what lets your Meteor app serve content to a webbrowser. It is included in the meteor-base set of packages that isautomatically added when you run meteor create. You can easily build aMeteor app without it - for example if you wanted to make a command-linetool that still used the Meteor package system and DDP.

This package also allows you to add handlers for HTTP requests.This lets other services access your app’s data through an HTTP API, allowingit to easily interoperate with tools and frameworks that don’t yet support DDP.

webapp exposes the connect API forhandling requests through WebApp.connectHandlers.Here’s an example that will let you handle a specific URL:

  1. // Listen to incoming HTTP requests (can only be used on the server).
  2. WebApp.connectHandlers.use('/hello', (req, res, next) => {
  3. res.writeHead(200);
  4. res.end(`Hello world from: ${Meteor.release}`);
  5. });

WebApp.connectHandlers.use([path], handler) has two arguments:

path - an optional path field.This handler will only be called on paths that matchthis string. The match has to border on a / or a .. For example, /hellowill match /hello/world and /hello.world, but not /hello_world.

handler - this is a function that takes three arguments:

  • req - a Node.jsIncomingMessageobject with some extra properties. This argument can be used to get informationabout the incoming request.
  • res - a Node.jsServerResponseobject. Use this to write data that should be sent in response to therequest, and call res.end() when you are done.
  • next - a function. Calling this function will pass on the handling ofthis request to the next relevant handler.