Q:

How to get the response data outside the post method in node js

USER.JS
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const user = express.Router();
var username='';
app.use(bodyParser.json());
user.post('/',  (req, res) => {
    let First_Name = req.body.First_Name;
    // This SQL query is vulnerable to SQL injection attacks. You should use parameters in this. 
    let query = "INSERT INTO `user` (First_Name) VALUES ('" + First_Name + "')";
    db.query(query, (err, result) => {
        if (err) { 
            return res.status(500).send(err);
        }
        res.send('Success');
        console.log(result);
        console.log('User Created successfully !');
        username=First_Name;
    })
})

module.exports = {user:user};

Middle.js
const app = express();
const bodyParser = require('body-parser');
const middle = express.Router();
const {user}= require('./USER');
middle.post('/', (req, res, next) => {
    //doing some code
    const username = 'here i need to use the username i get from user.js page';
    });
    module.exports = {middle:middle};

SERVER.JS
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const {user}= require('./routes/USER');
const {middle}= require('./routes/Middle');
app.use(bodyParser.json());
app.use('/add', user);// here i am adding the user 
app.use('/middle', middle);// here i need to call the middle which uses the username entered in user post
app.get('/', (req, res) => res.json('Working!'));

// Basic 404 handler
app.use((req, res) => {
    res.status(404).send('Not Found');
});
app.listen(port, () => {
    console.log(`Server running on port: ${port}`);
});

Here in middle.js file, i want to use the username get it from the user.js page. 
When a user enters the first name in react pages and submitted it is inserted into a database in USER.js page. I need to call the first_name from USER.js to the Middle.js page. I am calling both the apis in server.js page. How to maintain the workflow, like once the user inserted that value sends to middle post and doing some other functionality something like inserting into a .txt file? 

A:

I believe there is a simple solution to your problem, you wish to save username so you can use it in another function. 
The reason username is undefined in your  
console.log(username);// i need the variable here after insert outside function

line is that it will run on startup. So the POST has not occurred yet and of course the variable is not populated. 
You need to remember I/O in Node.js is asynchronous. So the user.post(...) handler will not block the main thread. 
NB: As @halfer pointed out, the SQL query you are using is a serious security risk. The First_Name should be passed as a query parameter.
The best way to deal with this is to pass username to another handler function.
I've also added an example of setting req.user in the user route.
Like below:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const user = express.Router();
var username='';
app.use(bodyParser.json());
app.use(user);
app.use(middleware);

user.post('/',  (req, res, next) => {
    let First_Name = req.body.First_Name;
    // This SQL query is vulnerable to SQL injection attacks. You should use parameters in this. 
    let query = "INSERT INTO `user` (First_Name) VALUES ('" + First_Name + "')";
    db.query(query, (err, result) => {
        if (err) { 
            return res.status(500).send(err);
        }
        res.send('Success');
        console.log(result);
        console.log('User Created successfully !');
        username=First_Name;
        userHandler(username);
        req.user = username;
        next();
    })
})

function middleware(req, res, next) {
    console.log("middleware: user:", req.user);
    next();
}

// Use this function to do whatever you wish with the user.
function userHandler(username) {
    console.log(`userHandler: Doing stuff with user: ${username}...`);
    /* Do whatever with the username variable... */
}

app.listen(7070);

