If you have an issue with
npm run watch
Do this
sudo pkill -f node
Create new file in root
router.js
javascript file communication
In router.js
console.log("I am executed immediately")
module.exports = "I am the export for the router file"
In app.js
below const app = express()
const router = require('./router')
Run the app and you will see that console.log line in the terminal.
The require function in node does two things:
- imports the file
- and returns what you set in module.exports in that file
in app.js below the const router try and log this variable to see what value you get
console.log(router)
You can export what you want in node. You can export an object also.
Try to return and object and log out its properties in app.js.
Clean out this demo code.
router.js
const express = require('express')
const router = express.Router()
This gives us a sort of a mini application
router.get('/', function(req, res){
res.render('home-guest')
})
module.exports = router
Delete the app.get function
app.use('/',router)
Check app it should be working in browser
In router.js create an about url and try that use res.send() for simple test