Answer the question
In order to leave comments, you need to log in
Can I run my middleware and query MYSQL in Nuxt universal mode (not spa)? How exactly?
Hello.
If I installed Nuxt as Universal, does that mean I can use the backend or not? And if I can - then how, for example, to make a query to the database?
I also tried to install with Express (I chose the option when installing Nuxt), but when I try to embed middleware in server/index.js - there are some incomprehensible errors - I didn’t find simple examples ...
Please explain exactly how I can use the server part in Nuxt. js to work with a database (mysql)?
At first I thought to make a separate Express on a different port, but now I decided that it would be a crutch - maybe it's possible somehow differently? Like Nuxt way))
Thank you.
Answer the question
In order to leave comments, you need to log in
I only study JS and NUXT, but as part of self-study, I did this.
I ask more experienced to point out the errors:
// nuxt.config.js
...
serverMiddleware: [
'~api/api'
],
...
//api.js
import {pool} from "./db";
const express = require('express');
const app = express();
app.get('/valute', function (req, res) {
const sql = "SELECT * FROM `valute-xml`";
pool.query(sql, function (error, results, fields) {
if (error) throw error;
res.json(results);
});
});
//db.js
const mysql = require('mysql2');
const pool = mysql.createPool({
connectionLimit: 100,
host: '',
user: '',
password: '',
database: ''
});
export {pool}
Nuxt is a front-end and server-side rendering. (server rendering != backend)
It does not perform backend tasks and is not made for this.
this is how it works, api on a separate domain / port, nuxt itself and accesses the api. API can be done with anything, not just JS.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question