Reputation: 33
I'm making a simple signup/login API with node and express as a backend but i want to know how to use nuxt as a front to connect to my APIs endpoints so i can depoloy my node app
Upvotes: 1
Views: 1944
Reputation: 14904
First thing is, you need to move to your nuxt.config.js
and you need to tell nuxt witch routes are for viewing content and witch are just API endpoints. For that there is a option called serverMiddleware
serverMiddleware: [
'~/api/index.js'
],
Now you need to create an Folder in your root directory called api
and inside the api
folder there you should create an file called index.js
, that will be your express.js server.
The index.js looks like this:
const express = require('express')
const app = express()
app.get("/test", (req, res) => {
res.status(200).json({ message: "I work" });
})
module.exports = {
path: '/api',
handler: app
}
Dont forget to install express npm i express
After that you need to restart your server.
If you navigate now to localhost:3000/api/test
you should see I work
I have also found out if you make for example a mistake, nuxt will tell you 404 cant find that page
. For example you write const express = require("express")
but you forget to install it via npm i express
, nuxt will just throw error 404
without saying that you dont installed express
Upvotes: 3