javascriptmysqlnode.jsexpressbackend

Using Multiple files for Different purpose


I am new to backend. I tried connecting mysql with node and wrote every get/post in the same file. When I try to use multiple file I get the error :

C:\Users\Administrator\Documents\NodeJs\node_modules\express\lib\router\route.js:211
        throw new Error(msg);
        ^

Error: Route.post() requires a callback function but got a [object Undefined]
    at Route.<computed> [as post] (C:\Users\Administrator\Documents\NodeJs\node_modules\express\lib\router\route.js:211:15)
    at app.<computed> [as post] (C:\Users\Administrator\Documents\NodeJs\node_modules\express\lib\application.js:499:19)
    at Object.<anonymous> (C:\Users\Administrator\Documents\NodeJs\register_user.js:18:5)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (C:\Users\Administrator\Documents\NodeJs\server.js:7:22)

This is my code for server.js used for connection

const express = require('express');
const app = express();
var parseUrl = require('body-parser');
let encodeUrl = parseUrl.urlencoded({extended: false});
const port = 3000;
const mysql = require('mysql2');
const registerUser = require('./register_user');
const con = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'',
    database:'register'
}
);

con.connect((err)=>{
    if (err) throw err;
    console.log("Database Connected");
});

app.listen(port,()=>{
    console.log(`Server running on port ${port}`);
});

module.exports = { con, encodeUrl};

And this is the code for register_user.js

const express = require('express');
const app = express();
const connectionImports = require("./server");
const con = connectionImports.con;
const encodeUrl = connectionImports.encodeUrl

app.get('/',(req,res)=>{
    res.sendFile(__dirname+'/register.html');
});

app.get('/register',(req,res)=>{
    // res.send(req.body);
    res.send("HElllo");
    console.log(res.body.firstName)
    console.log(res.body.password)
    console.log(res.body.email)
});
app.post('/register',encodeUrl,(req,res)=>{
    const firstname = req.body.firstName;
    const password = req.body.password;
    const email = req.body.email;
    res.send(`${firstname} ${password} ${email}`);
    const query = `Insert INTO register_student VALUES ('${email}','${password}','${firstname}')`;
    con.query(query,(err,result)=>{
        if(err) throw err;
        console.log(result);
    })});

Please also provide me some tips to improve my code.

I tried to add routers and different methods to fix this problem. I even tried running the register_user.js only at a time but it doesn't work.


Solution

  • You're encountering the error because you're importing a route handler from another file, but that handler is likely either:

    Not exported correctly, or

    Not imported correctly, or

    Not defined properly before being used