Search code examples
javascriptexpressnode-moduleses6-modules

Using ES6 import to import and use express router in single line


What's the one line equivalent of below statement using ES6 Import

My existing code (using require)

app.use('/c', require('./route/routesc')); // routes which are common
app.use('/d', require('./route/routesd')); // routes for Data Agent
app.use('/a', require('./route/routesa')); // routes for Admin

Same code using ES6 Import

import routesc from "./route/routesc.js"
import routesd from "./route/routesd.js"
import routesa from "./route/routesa.js"

app.use('/c', routesc); // routes which are common
app.use('/d', routesd); // routes for Data Agent
app.use('/a', routesa); // routes for Admin

I am wondering is there any ONE liner for import?

FYI, I spent 40 mins searching for this answer and finally posted here on SO.


Solution

  • The ONE-liner equivalent would be

    import routesc from "./route/routesc.js"; app.use('/c', routesc); // routes which are common
    import routesd from "./route/routesd.js"; app.use('/d', routesd); // routes for Data Agent
    import routesa from "./route/routesa.js"; app.use('/a', routesa); // routes for Admin
    

    There is no way to combine them into a single statement, an import declaration always needs to stand on its own.