Giter Club home page Giter Club logo

nuxt-auth-routes-on-express's Introduction

nuxt-auth-routes-on-express

Nuxt auth-routes example ported to express server run universal Nuxt app

Instructions for use

  • Copy .env.example in the docroot to .env and enter a fully qualified domain name or IP for the express api server (which in this case is the same server that is serving up Nuxt itself).
    • 'localhost' or '0.0.0.0' didn't work for me
  • Then the standard Nuxt commands work as usual
  • Please read the third section where I explain how this app was created.

Build Setup

# install dependencies
$ npm install

# serve with hot reload at localhost:3000
$ npm run dev

# build for production and launch server
$ npm run build
$ npm start

# generate static project
$ npm run generate

For detailed explanation on how things work, checkout Nuxt.js docs.

How this app was made

Having discovered create-nuxt-app I was anxious to try out the great flexibility and power offered there. So my objective was:

  • Use create-nuxt-app to generate an independent express app (in the ./server directory) which actually sets up a series of independent api routes which operate outside of nuxt, and which will then bring up the nuxt app itself.
  • Port the Nuxt.js auth-routes example to this new setup. Not as ServerMiddleware on the usual Nuxt stack, but rather connecting to an express server which for all intents and purposes is external and independent and capable of doing all kinds of things.
  • The endpoints should be reachable from the regular Nuxt universal app, and also externally, via curl or Postman.

Steps taken:

vk@example:~/nuxt/try-stuff$ create-nuxt-app nuxt-auth-routes-on-express
> Generating Nuxt.js project in /home/vk/nuxt/try-stuff/nuxt-auth-routes-on-express
? Project name nuxt-auth-routes-on-express
? Project description My beautiful Nuxt.js project
? Use a custom server framework express
? Use a custom UI framework vuetify
? Choose rendering mode Universal
? Use axios module yes
? Use eslint yes
? Author name (victorkane)
? Choose a package manager npm
Initialized empty Git repository in /home/vk/nuxt/try-stuff/nuxt-auth-routes-on-express/.git/
added 1303 packages in 58.553s

  To get started:

    cd nuxt-auth-routes-on-express
    npm run dev

  To build & start for production:

    cd nuxt-auth-routes-on-express
    npm run build
    npm start

vk@example:~/nuxt/try-stuff$

Interesting to see how the package.json scripts section changes from what we are used to. The npm run dev actually executes:

"dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server",

I then proceeded as follows:

  • package.json
    • same dependencies (express, body-parser, express-session, nuxt) except we are using the nuxt axios module and they are using the npm dependency. Also we have vuetify.
  • nuxt-config
    • no servermiddleware or anything, since express is in control anyway (is that right?)
  • express code
    • in their's, it's in the ServerMiddleware, in ours it's under the server directory, no need for declaration as ServerMiddleware cuz it runs Nuxt and not the inverse.
    • for the same reason, ours is not a module and need not export any variables for ServerMiddleware
    • they have this req, res transformation thingie
    • we add in express-session options in the code
  • middleware: auth.js (copy as is)
  • store (copy as is, but adjust for $axios mod
  • pages
    • index.vue merged in
    • secret.vue copied in

The end result works. First of all, curl can be used right from the command line to access the login API, as if it were an entirely separate express server app, which, actually, it is:

curl --request POST \
  --url http://localhost:4300/api/login \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --data '{
    "username": "victorkane",
    "email": "[email protected]",
    "password": "password"
}'
{"data":{"username":"victorkane"}}

curl --request POST \
  --url http://localhost:4300/api/login \
  --header 'accept: application/json' \
  --header 'content-type: application/json' \
  --data '{
    "username": "demo",
    "email": "[email protected]",
    "password": "demo"
}'

And the app works as advertised:

  • From the Vuetify Welcome page, fill in the form with "demo" + "demo"
  • Works like the original auth-routes example and demo. You can even refresh the browser and you're still logged in.

Please send any (welcome) feedback in the issue queue.

nuxt-auth-routes-on-express's People

Contributors

victorkane avatar

Watchers

James Cloos avatar athiwatp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.