Skip to content

Instantly share code, notes, and snippets.

@silvestreh
Last active August 21, 2019 17:19
Show Gist options
  • Save silvestreh/0be8c9575dc71e84117de338b7396d5f to your computer and use it in GitHub Desktop.
Save silvestreh/0be8c9575dc71e84117de338b7396d5f to your computer and use it in GitHub Desktop.
Integrate Nuxt into Feathers
// src/index.js
/* eslint-disable no-console */
const logger = require('winston');
const app = require('./app');
const port = app.get('port');
process.on('unhandledRejection', (reason, p) => {
logger.error('Unhandled Rejection at: Promise ', p, reason);
});
process.on('nuxt:build:done', (err) => {
if (err) {
logger.error(err);
}
const server = app.listen(port);
server.on('listening', () => {
logger.info(`Feathers application started on ${app.get('host')}:${port}`);
});
});
// src/app.js
/**
* There's not much to do here. Just remove or comment out
* the lines that use feathers.static (the public/ directory)
*/
app.use('/', feathers.static(app.get('public'))); // Find and remove this line
# Remember to install the new dependencies
npm install --save nuxt
npm install --save-dev nodemon
// src/middleware/index.js
/**
* Add the Nuxt middleware to your Feathers app
*/
const handler = require('feathers-errors/handler');
const notFound = require('feathers-errors/not-found');
const nuxt = require('./nuxt'); // Require the middleware
module.exports = function () {
// Add your custom middleware here. Remember, that
// in Express the order matters, `notFound` and
// the error handler have to go last.
const app = this;
app.use(nuxt.render); // Use the middleware. Remember to do it before the notFound and error handler middlewares
app.use(notFound());
app.use(handler());
};
/**
* Set up Nuxt – More info at: https://nuxtjs.org/guide/configuration/
*/
const path = require('path');
module.exports = {
loading: {
color: '#92D3CE',
},
rootDir: path.resolve(__dirname),
dir: path.resolve(__dirname),
dev: process.env.NODE_ENV !== 'production',
};
// src/middleware/nuxt.js
/**
* Create the Nuxt middleware. This builds Nuxt and exports it
*/
const Nuxt = require('nuxt');
const config = require('../../nuxt.config');
const logger = require('winston');
const nuxt = new Nuxt(config);
if (config.dev) {
nuxt.build()
.then(() => process.emit('nuxt:build:done'))
.catch((error) => {
logger.error(error);
process.exit(1);
});
} else {
process.nextTick(() => process.emit('nuxt:build:done'));
}
module.exports = nuxt;
// Add new scripts for `dev` and `build` to your package.json
{
"scripts": {
"test": "npm run eslint && npm run mocha",
"eslint": "eslint src/. test/. --config .eslintrc.json",
"start": "node src/",
"build": "nuxt build",
"dev": "DEBUG=nuxt:* nodemon --watch src/ --watch config/ src/index.js",
"mocha": "mocha test/ --recursive"
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment