BETA

Example Deployment with Now v1

This deployment example refers to the Now serverless deployment, specifically deployments using Docker containers.

This example refers to Now v1, which has been deprecated. If you are looking for Now v2, check out the Now v2 example.

Prerequisites

Before you get started, you need to have:

We're going to deploy the application under the domain mc-examples-starter.now.sh.

The mc-examples-starter.now.sh domain is already in use. Pick a different one if you choose to deploy using Now.

Configuration

The first thing to do is to define a now.json file to configure the deployment with the following JSON:

{
"version": 1,
"public": true,
"name": "mc-examples-starter",
"alias": "mc-examples-starter",
"regions": ["bru"],
"files": ["dist", "headers.json", "env.prod.json", "now.json", "package.json"]
}

Some fields may vary based on your setup and requirements, for example public, regions, etc.

The files field determines the list of files for uploading to the cloud servers. The env.prod.json file, contains the configuration for production use, to differentiate it from the env.json used for development.

Next, we need to create an env.prod.json file with the following JSON:

{
"applicationName": "mc-examples-starter",
"frontendHost": "mc-examples-starter.now.sh",
"mcApiUrl": "https://mc-api.commercetools.com",
"location": "gcp-eu",
"env": "production",
"cdnUrl": "http://mc-examples-starter.now.sh",
"servedByProxy": true
}

We also need a headers.prod.json to whitelist the domains for the Content Security Policy:

{
"csp": {
"script-src": ["mc-examples-starter.now.sh"],
"connect-src": [
"mc-examples-starter.now.sh",
"mc-api.commercetools.com"
],
"style-src": ["mc-examples-starter.now.sh"]
}
}

As a next step, we need a Dockerfile which contains the runtime configuration for starting the HTTP server:

FROM node:10-stretch
WORKDIR /app
COPY ./dist /app/dist
COPY ./package.json /app/
RUN yarn install --production
COPY ./csp.json /app/
COPY ./env.prod.json /app/
ENV NODE_ENV=production
EXPOSE 3001
CMD /app/node_modules/.bin/mc-http-server --config /app/env.prod.json --csp /app/csp.json --use-local-assets

For the sake of the example, the setup includes the static assets within the HTTP server as described here. For real production use we recommend uploading static assets to a CDN or Cloud Storage as described here.

We also assume that the assets are built first with yarn build and then copied into the Docker image (COPY ./dist /app/dist).

Deployment

Finally, we can trigger the deployment using the Now CLI:

yarn build
now
now alias

Now deploy - part 1 Now deploy - part 2 Now alias

After changing to https://mc-examples-starter.now.sh we can see the application loading and inspect the Developer Console to see that the configuration is correct:

Now deployed

However, if you try to log in, you will get an error:

Now login failed

This is expected. The domain mc-examples-starter.now.sh is not allowed by the CORS rules defined in the Merchant Center API Gateway. It's a security measure as the Custom Application should only be served behind the Merchant Center proxy.

Now you're ready to Register your Custom Application and start using it!

Developer Center
HTTP APIGraphQL APIBETAPlatform Release NotesCustom ApplicationsBETASDKs & Client LibrariesImport & Export ToolsSUNRISE Starter FrontendsTutorialsFAQ
Merchant Center
DocumentationRelease Notes
Copyright © 2020 commercetools