Requirements

  • It is required that you select a backend when setting up the project. Even if you don’t need it, or else the site won’t start up.
  • The server is running Node 8 or greater

What if I already have a project without an backend?

No worries. It is easy to add an express server to an existing project.

Create a new folder called server in the root of the project. Then create an index.js file inside the server folder and paste the following inside the index.js:

  1. const express = require('express')
  2. const consola = require('consola')
  3. const { loadNuxt } = require('nuxt-start')
  4. const app = express()
  5. async function start () {
  6. const nuxt = await loadNuxt(isDev ? 'dev' : 'start')
  7. await nuxt.listen(process.env.PORT, process.env.HOST)
  8. }
  9. start()

Then edit your nuxt.config.js:

Before:

  1. import pkg from './package'
  2. export default {
  3. ... config
  4. }

After:

  1. module.exports = {
  2. ... config
  3. }

Remember to remove the references to the pkg object inside the config.

That’s it!

For an Azure App Service deployment, make sure you set the following two environment variables (application settings) in App Service › Settings › Configuration › Application settings.

  1. HOST: '0.0.0.0'
  2. NODE_ENV: 'production'

How to set Node version on Web App in DevOps

You can set the Node version on the server, via the App setting inside the “Deploy Azure Web Service” task in the release pipeline

Add this to the App settings field under “Application and Configuration Settings”

  1. -WEBSITE_NODE_DEFAULT_VERSION 10.16.3

It’s recommended to use the LTS version.

Artifacts

If you are using Azure DevOps and let the build pipeline do its work you and want to store artifacts. Files which are prefixed with a . must be moved to the artifact folder explicitly. Then you can create an Artifact Archive and download it afterwards in your Release Deployment.

Running the webserver

For Azure Portal you will need a web.config file. If not supplied, it will create one itself. This one won’t work for Nuxt though. Add a web.config file to your repository. For the latest version of Nuxt the server file is located at server/index.js. In the web.config you don’t specify the exact path server/index.js but just server. See the example web.config below. If you don’t do this the logs will tell you that Vue cannot find any routes.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. This configuration file is required if iisnode is used to run node processes behind
  4. IIS or IIS Express. For more information, visit:
  5. https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
  6. -->
  7. <configuration>
  8. <system.webServer>
  9. <!-- Visit https://azure.microsoft.com/en-us/blog/introduction-to-websockets-on-windows-azure-web-sites/ for more information on WebSocket support -->
  10. <webSocket enabled="false" />
  11. <handlers>
  12. <!-- Indicates that the server.js file is a Node.js site to be handled by the iisnode module -->
  13. <add name="iisnode" path="server" verb="*" modules="iisnode"/>
  14. </handlers>
  15. <rewrite>
  16. <rules>
  17. <!-- Do not interfere with requests for node-inspector debugging -->
  18. <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
  19. <match url="^server\/debug[\/]?" />
  20. </rule>
  21. <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
  22. <rule name="StaticContent">
  23. <action type="Rewrite" url="public{REQUEST_URI}"/>
  24. </rule>
  25. <!-- All other URLs are mapped to the Node.js site entry point -->
  26. <rule name="DynamicContent">
  27. <conditions>
  28. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
  29. </conditions>
  30. <action type="Rewrite" url="server"/>
  31. </rule>
  32. </rules>
  33. </rewrite>
  34. <!-- 'bin' directory has no special meaning in Node.js and apps can be placed in it -->
  35. <security>
  36. <requestFiltering>
  37. <hiddenSegments>
  38. <remove segment="bin"/>
  39. </hiddenSegments>
  40. </requestFiltering>
  41. </security>
  42. <!-- Make sure error responses are left untouched -->
  43. <httpErrors existingResponse="PassThrough" />
  44. <!--
  45. You can control how Node is hosted within IIS using the following options:
  46. * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
  47. * node_env: will be propagated to node as NODE_ENV environment variable
  48. * debuggingEnabled - controls whether the built-in debugger is enabled
  49. See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
  50. -->
  51. <!--<iisnode watchedFiles="web.config;*.js"/>-->
  52. </system.webServer>
  53. </configuration>