First, the server file app.js. (To use the route file)
const express = require('express')
const app = express()
const swig = require('swig')
const bodyParser = require('body-parser');
var mainRouter = require('./routers/main')
var apiRouter = require('./routers/api')
app.listen(3001,()=>{
console.log('http://localhost:3001');
})
/**
* 1, the static file hosting
*/
app.use ( '/ public', express.static (__ dirname + '/ public')) // [directory] Set static hosting
/**
* 2, set up to resolve
*/
app.use(bodyParser.urlencoded({ extended: true }))
/**
* 3, the configuration template engine
*/
app.engine('html', swig.renderFile)
app.set('views', './views')
app.set('view engine', 'html')
swig.setDefaults({ cache: false })
/**
* 4, configure routing
*/
app.use('/', mainRouter)
app.use('/api', apiRouter)
Second, the routing file main.js
var express = require('express')
var router = express.Router()
/**
* Home
*/
router.get('/',(req,res)=>{
res.render('main/index
.html')
})
module.exports = router