Repository: nuxt/express Branch: master Commit: 0550443afb93 Files: 25 Total size: 16.0 KB Directory structure: gitextract_mbkkqpqj/ ├── .editorconfig ├── .eslintrc.js ├── .gitignore ├── README.md ├── api/ │ ├── index.js │ └── routes/ │ ├── test.js │ └── users.js ├── assets/ │ └── README.md ├── components/ │ ├── Logo.vue │ └── README.md ├── layouts/ │ ├── README.md │ ├── default.vue │ └── error.vue ├── middleware/ │ └── README.md ├── nuxt.config.js ├── package.json ├── pages/ │ ├── README.md │ ├── index.vue │ └── users/ │ ├── _id.vue │ └── index.vue ├── plugins/ │ └── README.md ├── protected-ssr-api.md ├── renovate.json ├── static/ │ └── README.md └── store/ └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ # editorconfig.org root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.md] trim_trailing_whitespace = false ================================================ FILE: .eslintrc.js ================================================ module.exports = { root: true, env: { browser: true, node: true }, parserOptions: { parser: 'babel-eslint' }, extends: [ '@nuxtjs', 'plugin:nuxt/recommended' ], plugins: [ ], // add your custom rules here rules: {} } ================================================ FILE: .gitignore ================================================ # Created by .ignore support plugin (hsz.mobi) ### Node template # Logs /logs *.log npm-debug.log* yarn-debug.log* yarn-error.log* # Runtime data pids *.pid *.seed *.pid.lock # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # nyc test coverage .nyc_output # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # Bower dependency directory (https://bower.io/) bower_components # node-waf configuration .lock-wscript # Compiled binary addons (https://nodejs.org/api/addons.html) build/Release # Dependency directories node_modules/ jspm_packages/ # TypeScript v1 declaration files typings/ # Optional npm cache directory .npm # Optional eslint cache .eslintcache # Optional REPL history .node_repl_history # Output of 'npm pack' *.tgz # Yarn Integrity file .yarn-integrity # dotenv environment variables file .env # parcel-bundler cache (https://parceljs.org/) .cache # next.js build output .next # nuxt.js build output .nuxt # Nuxt generate dist # vuepress build output .vuepress/dist # Serverless directories .serverless # IDE / Editor .idea # Service worker sw.* # macOS .DS_Store # Vim swap files *.swp ================================================ FILE: README.md ================================================ # Nuxt 2 with Express > [ExpressJS](http://expressjs.com/) + [Nuxt 2](https://v2.nuxt.com) = :zap: Live Demo: [https://codesandbox.io/s/github/nuxt-community/express-template](https://codesandbox.io/s/github/nuxt-community/express-template) ## Nuxt 3 Nuxt 3 is powered by [unjs/h3](https://github.com/unjs/h3) which has a compatible API with Express and is much faster with the ability to run in Workers ([read more about it](https://nuxt.com/blog/nuxt-on-the-edge)). This is why this template won't be migrated to Nuxt 3. ## Installation This is a template project, click on the green button "Use this template" at the top of this page and get started with GitHub :sparkles: One you cloned your repository, install the dependencies with: ```bash yarn install # or npm install ``` ## ExpressJS Changes - There is a `api` directory with the root of your `api` server. - The `routes` directory is called `api/routes`. ## Commands | Command | Description | |---------|-------------| | npm run dev | Start ExpressJS server in development with Nuxt.js in dev mode (hot reloading). Listen on [http://localhost:3000](http://localhost:3000). | | npm run build | Build the nuxt.js web application for production. | | npm start | Start ExpressJS server in production. | ## 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) - [Nuxt.js](https://nuxtjs.org/guide/) - [Vue.js](http://vuejs.org/guide/) ## Licenses - [ExpressJS license](https://github.com/expressjs/express/blob/master/LICENSE) - [NuxtJS license](https://github.com/nuxt/nuxt.js/blob/master/LICENSE.md) - [VueJS license](https://github.com/vuejs/vue/blob/master/LICENSE) ================================================ FILE: api/index.js ================================================ const express = require('express') // Create express instance const app = express() // Require API routes const users = require('./routes/users') const test = require('./routes/test') // Import API Routes app.use(users) app.use(test) // Export express app module.exports = app // Start standalone server if directly running if (require.main === module) { const port = process.env.PORT || 3001 app.listen(port, () => { // eslint-disable-next-line no-console console.log(`API server listening on port ${port}`) }) } ================================================ FILE: api/routes/test.js ================================================ const { Router } = require('express') const router = Router() // Test route router.use('/test', (req, res) => { res.end('Test API!') }) module.exports = router ================================================ FILE: api/routes/users.js ================================================ const { Router } = require('express') const router = Router() // Mock Users const users = [ { name: 'Alexandre' }, { name: 'Pooya' }, { name: 'Sébastien' } ] /* GET users listing. */ router.get('/users', function (req, res, next) { res.json(users) }) /* GET user by ID. */ router.get('/users/:id', function (req, res, next) { const id = parseInt(req.params.id) if (id >= 0 && id < users.length) { res.json(users[id]) } else { res.sendStatus(404) } }) module.exports = router ================================================ FILE: assets/README.md ================================================ # ASSETS **This directory is not required, you can delete it if you don't want to use it.** This directory contains your un-compiled assets such as LESS, SASS, or JavaScript. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/assets#webpacked). ================================================ FILE: components/Logo.vue ================================================ ================================================ FILE: components/README.md ================================================ # COMPONENTS **This directory is not required, you can delete it if you don't want to use it.** The components directory contains your Vue.js Components. _Nuxt.js doesn't supercharge these components._ ================================================ FILE: layouts/README.md ================================================ # LAYOUTS **This directory is not required, you can delete it if you don't want to use it.** This directory contains your Application Layouts. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/views#layouts). ================================================ FILE: layouts/default.vue ================================================ ================================================ FILE: layouts/error.vue ================================================ ================================================ FILE: middleware/README.md ================================================ # MIDDLEWARE **This directory is not required, you can delete it if you don't want to use it.** This directory contains your application middleware. Middleware let you define custom functions that can be run before rendering either a page or a group of pages. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/routing#middleware). ================================================ FILE: nuxt.config.js ================================================ export default { /* ** Nuxt target ** See https://nuxtjs.org/api/configuration-target */ target: 'server', /* ** Headers of the page ** See https://nuxtjs.org/api/configuration-head */ head: { title: process.env.npm_package_name || '', meta: [ { charset: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' }, { hid: 'description', name: 'description', content: process.env.npm_package_description || '' } ], link: [ { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' } ] }, /* ** Global CSS */ css: [ ], /* ** Plugins to load before mounting the App ** https://nuxtjs.org/guide/plugins */ plugins: [ ], /* ** Auto import components ** See https://nuxtjs.org/api/configuration-components */ components: true, /* ** Nuxt.js dev-modules */ buildModules: [ // Doc: https://github.com/nuxt-community/eslint-module '@nuxtjs/eslint-module' ], /* ** Nuxt.js modules */ modules: [ // Doc: https://http.nuxtjs.org '@nuxt/http' ], /* ** Server Middleware */ serverMiddleware: { '/api': '~/api' }, /* ** For deployment you might want to edit host and port */ // server: { // port: 8000, // default: 3000 // host: '0.0.0.0' // default: localhost // }, /* ** Build configuration ** See https://nuxtjs.org/api/configuration-build/ */ build: { } } ================================================ FILE: package.json ================================================ { "name": "nuxt-express", "version": "1.0.0", "private": true, "scripts": { "dev": "nuxt", "build": "nuxt build", "start": "nuxt start", "generate": "nuxt generate", "lint": "yarn lint:js", "lint:js": "eslint --ext .js,.vue --ignore-path .gitignore ." }, "dependencies": { "@nuxt/http": "latest", "express": "latest", "nuxt": "latest" }, "devDependencies": { "@nuxtjs/eslint-config": "^5.0.0", "@nuxtjs/eslint-module": "^3.0.2", "babel-eslint": "^10.1.0", "eslint": "^7.20.0", "eslint-plugin-nuxt": "^2.0.0" } } ================================================ FILE: pages/README.md ================================================ # PAGES This directory contains your Application Views and Routes. The framework reads all the `*.vue` files inside this directory and creates the router of your application. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/routing). ================================================ FILE: pages/index.vue ================================================ ================================================ FILE: pages/users/_id.vue ================================================ ================================================ FILE: pages/users/index.vue ================================================ ================================================ FILE: plugins/README.md ================================================ # PLUGINS **This directory is not required, you can delete it if you don't want to use it.** This directory contains Javascript plugins that you want to run before mounting the root Vue.js application. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/plugins). ================================================ FILE: protected-ssr-api.md ================================================ 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'] } ``` ================================================ FILE: renovate.json ================================================ { "extends": [ "@nuxtjs" ] } ================================================ FILE: static/README.md ================================================ # STATIC **This directory is not required, you can delete it if you don't want to use it.** This directory contains your static files. Each file inside this directory is mapped to `/`. Thus you'd want to delete this README.md before deploying to production. Example: `/static/robots.txt` is mapped as `/robots.txt`. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/assets#static). ================================================ FILE: store/README.md ================================================ # STORE **This directory is not required, you can delete it if you don't want to use it.** This directory contains your Vuex Store files. Vuex Store option is implemented in the Nuxt.js framework. Creating a file in this directory automatically activates the option in the framework. More information about the usage of this directory in [the documentation](https://nuxtjs.org/guide/vuex-store).