diff --git a/README.md b/README.md index 0015c5b..20d6ba7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This is a project template for [vue-cli](https://github.com/vuejs/vue-cli). ```bash -vue init nuxt/express +vue init nuxt-community/express-template cd # move to your project npm install # or yarn install ``` @@ -19,7 +19,8 @@ npm install # or yarn install ## ExpressJS Changes -- The `routes` directory is called `api`. +- There is a `server` directory with the root of your `express` server. +- The `routes` directory is called `server/api`. ## Commands @@ -29,10 +30,18 @@ npm install # or yarn install | npm run build | Build the nuxt.js web application for production. | | npm start | Start ExpressJS server in production. | +## Backpack + +We use [backpack](https://github.com/palmerhq/backpack) to watch and build the application, so you can use the latest ES6 features (module syntax, async/await, etc.). + ## Live Demo [https://express.nuxtjs.org](https://express.nuxtjs.org) +## Examples + +- [Handling Protected SSR Routes](https://github.com/nuxt/express/blob/master/protected-ssr-api.md) + ## Documentation - [ExpressJS](http://expressjs.com/en/guide/routing.html) diff --git a/protected-ssr-api.md b/protected-ssr-api.md new file mode 100644 index 0000000..b858c21 --- /dev/null +++ b/protected-ssr-api.md @@ -0,0 +1,27 @@ +The moment you start dealing with user session, you'll notice that protected routes will reject nuxt requests when running from the server. This is because when nuxt is ran from the server, the browser cookie is lost. Below is a quick middleware solution to inject the browser's cookie to your ssr axios request. + +## install session + +`npm install --save express-session` + + +## create middleware/ssr-cookie.js + +```js +import axios from '~plugins/axios' + +export default function({isServer, req}) { + if (isServer) { + axios.defaults.headers.common.cookie = req.headers.cookie + } +} +``` + +## add middleware to nuxt.config.js + +```js +router: { + middleware: ['ssr-cookie'] +} +``` + diff --git a/template/.eslintrc.js b/template/.eslintrc.js index 9a11039..b44e099 100644 --- a/template/.eslintrc.js +++ b/template/.eslintrc.js @@ -11,15 +11,6 @@ module.exports = { 'html' ], // add your custom rules here - rules: { - // allow paren-less arrow functions - 'arrow-parens': 0, - // allow async-await - 'generator-star-spacing': 0, - // allow debugger during development - 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0, - // do not allow console.logs etc... - 'no-console': 2 - }, + rules: {}, globals: {} } diff --git a/template/.gitignore b/template/.gitignore index 7ee6115..7b356cd 100644 --- a/template/.gitignore +++ b/template/.gitignore @@ -9,3 +9,6 @@ npm-debug.log # Nuxt generate dist + +# Backpack build +build diff --git a/template/README.md b/template/README.md index d58a87f..fe3d0e1 100644 --- a/template/README.md +++ b/template/README.md @@ -13,5 +13,10 @@ $ npm run dev # build for production and launch server $ npm start +``` For detailed explanation on how things work, checkout the [Nuxt.js docs](https://github.com/nuxt/nuxt.js). + +## Backpack + +We use [backpack](https://github.com/palmerhq/backpack) to watch and build the application, so you can use the latest ES6 features (module syntax, async/await, etc.). diff --git a/template/api/index.js b/template/api/index.js deleted file mode 100644 index a1b390f..0000000 --- a/template/api/index.js +++ /dev/null @@ -1,6 +0,0 @@ -var router = require('express').Router() - -// Add USERS Routes -router.use(require('./users')) - -module.exports = router diff --git a/template/backpack.config.js b/template/backpack.config.js new file mode 100644 index 0000000..2d45032 --- /dev/null +++ b/template/backpack.config.js @@ -0,0 +1,6 @@ +module.exports = { + webpack: (config, options, webpack) => { + config.entry.main = './server/index.js' + return config + } +} diff --git a/template/layouts/default.vue b/template/layouts/default.vue index fa5e8d6..c9d50bd 100644 --- a/template/layouts/default.vue +++ b/template/layouts/default.vue @@ -6,7 +6,7 @@