我有一个基本的NUXT应用程序正在运行,在安装过程中,我选择包括集成的Express Server。
我设法将SQL Server数据库连接到Express(包括Windows身份验证,是的!)
我正在努力获取网址以返回数据。使用邮递员到达终点将返回nuxt的内置404错误页面。
如何配置NUXT来从查询中返回数据?
nuxt.config.js
const colors = require('vuetify/es5/util/colors').default
module.exports = {
mode: 'universal',
/*
** Headers of the page
*/
head: {
titleTemplate: '%s - ' + process.env.npm_package_name,
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' }]
},
/*
** Customize the progress-bar color
*/
loading: { color: '#fff' },
/*
** Global CSS
*/
css: [],
/*
** Plugins to load before mounting the App
*/
plugins: [],
/*
** Nuxt.js dev-modules
*/
buildModules: [
// Doc: https://github.com/nuxt-community/eslint-module
'@nuxtjs/eslint-module',
'@nuxtjs/vuetify'
],
/*
** Nuxt.js modules
*/
modules: [
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios'
],
/*
** Axios module configuration
** See https://axios.nuxtjs.org/options
*/
axios: {},
/*
** vuetify module configuration
** https://github.com/nuxt-community/vuetify-module
*/
vuetify: {
customVariables: ['~/assets/variables.scss'],
theme: {
dark: true,
themes: {
dark: {
primary: '#007B5F',
accent: '#007FA3',
secondary: '#33957F',
info: colors.teal.lighten1,
warning: colors.amber.base,
error: colors.deepOrange.accent4,
success: colors.green.accent3
}
}
}
},
/*
** Build configuration
*/
build: {
/*
** You can extend webpack config here
*/
extend(config, ctx) {}
}
}
这些文件在根服务器文件夹中
index.js
const express = require('express')
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')
const app = express()
// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = process.env.NODE_ENV !== 'production'
// Add api routes
const bridgeRoute = require('./routes/bridgeRoute')
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config)
const { host, port } = nuxt.options.server
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
} else {
await nuxt.ready()
}
// Give nuxt middleware to express
app.use(nuxt.render)
// Use included routes
app.use('/api', bridgeRoute)
// Listen the server
app.listen(port, host)
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
})
}
start()
db.js
const sql = require('mssql/msnodesqlv8')
const consola = require('consola')
const dbConfig = require('./dbConfig')
// Swap between 'LOCAL', 'TEST' and 'PRODUCTION' environments
const connString = dbConfig.dbConfig.LOCAL.connectionString
const poolPromise = new sql.ConnectionPool(connString)
.connect()
.then((pool) => {
consola.success('Connected to MSSQL')
return pool
})
.catch((err) =>
consola.error('Database Connection Failed! Bad Config: ', err)
)
module.exports = {
sql,
poolPromise
}
dbConfig仅包含连接字符串以连接到SQL Server
/ routes / bridgeRoute.js
const express = require('express')
const router = express.Router()
const { poolPromise } = require('../db')
router.get('api/bridges', async (req, res) => {
try {
const pool = await poolPromise
const result = await pool.request().query('SELECT TOP 10 FROM [BRIDGE]')
res.json(result)
} catch (err) {
res.status(500)
res.send(err.message)
}
})
module.exports = router
我正在从事类似的项目,您有没有发现这个问题?没有13个错误,我似乎都无法将mssql导入nuxt应用]