-
Hi, const path = require( 'path' )
const fastify = require( 'fastify' )( { logger: false } )
const lgn = require( './logas' ) // mine login js file
const conStr = 'mysql://root:pasw@localhost:3306/mineDB'
fastify.register( require( '@fastify/static' ),
{
root: path.join( __dirname, 'public' ),
prefix: '/public/'
})
fastify.register( require( '@fastify/formbody' ) )
fastify.register( require( '@fastify/mysql' ), { connectionString: conStr } )
fastify.register( lgn )
fastify.get( '/', ( req, res ) =>
{
res.sendFile( 'login.html' )
})
fastify.post( '/login', ( req, res ) =>
{
const vart = req.body.user
const slap = req.body.password
const ret = fastify.prisijungt( { vrt: vart, slp: slap } )
if ( ret )
res.sendFile( 'index.html' )
})
fastify.get( '/about', ( req, res ) =>
{
res.sendFile( 'about.html' )
})
fastify.listen({ port: 8080 }, err =>
{
if ( err ) throw err
console.log( `Server run - ${fastify.server.address().port}` )
}) logas.js file : function prisijungt( fastify, prm, done )
{
const qryStr = "SELECT * FROM users WHERE usr_id = ? and usr_psw = ? and allw = 1 LIMIT 1;"
fastify.mysql.query( qryStr, [ prm.vrt, prm.slp ], function onResult ( err, result )
{
return ( err || result )
})
done()
}
module.exports = prisijungt Mine problem is that _ get an error on 'post', where is "fastify.prisijungt" function, registered as plugin : What is wrong here ? Please help ... :( Many thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 4 replies
-
Please use |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer! Ok, tried that from samples in internet :
The same error ... :( Can you share with some sample ? |
Beta Was this translation helpful? Give feedback.
-
I'll be very thankful for any source sample ... :) |
Beta Was this translation helpful? Give feedback.
-
Now I found that - https://blog.rafaelgss.com.br/fastify-why-another-framework-js |
Beta Was this translation helpful? Give feedback.
-
Maybe I'm stupid, but after reading that I can't to do that work ... :( Of course I can query from mysql inside post request - and that is working. But ... the plugins sooner or later it will be required ... Can somebody to correct mine piece of source and explain what is wrong here ? I'm in learning process of Fastify - but documentation in most is very elementary - plugins in the same js. If in the outside js - very simplest functions ... How to call plugin with mysql query and return results ? Thanks in advance ... |
Beta Was this translation helpful? Give feedback.
-
A plugin is used to decorate the Fastify objects (server, request, reply). Your "plugin" is a one time function that does a query and returns. All of our documentation clearly shows how to write plugins that decorate the Fastify objects with reusable functions. |
Beta Was this translation helpful? Give feedback.
-
Jsummers, thank for the answer ... Read this - https://www.fastify.io/docs/latest/Reference/Plugins/ At the end is a sample ...
Now it's working :) ... Tfuuuu ... I can say that at this moment fully don't understand how and why ... :) . But after some time maybe it will more understandly Anyway thanks ... |
Beta Was this translation helpful? Give feedback.
Jsummers, thank for the answer ...
Read this - https://www.fastify.io/docs/latest/Reference/Plugins/
At the end is a sample ...
Did own logas.js
Now it's working :) ... Tfuuuu ... I can say that at this moment fully don't understand how and why…