Hey r/node,I have a pretty basic static file server with a folder structure like this:root - routes - controllers - public - views - app.jsthe app.js looks like this:```javascript const express = require('express');const app = express(); const morgan = require('morgan');const path = require('path'); app.use(express.static('public'));// why don't I need to use views? app.use(morgan('dev')); const mainRoutes = require('./routes/index.js');app.use('/', mainRoutes); /* HANDLING ERRORS */app.use((req, res) => {res.status(404);res.sendFile(path.join(__dirname + '/views/404.html'));}); app.listen(5000);console.log("Server running at http://localhost:5000```And here's the controller```javascriptconst path = require('path'); exports.homePage = (req, res) => {console.log(req);res.sendFile(path.join(__dirname + '/index.html'));}```If I want to serve the "about" page which will be in the public folder and the function to do so is in the controller folder (index.js) how do I have to do it? Because I tried using the same method as for the index.html and, of course, __dirname was "controllers"... Why does it work then with index.html? There's also the routes folder but it is pretty straight forward as you can imagine, just requiring functions from the controllers. Thanks, hope I was clear!
Submitted June 14, 2020 at 09:53AM by _folgo_
No comments:
Post a Comment