Reputation: 31
I'm developing a blog to learn node and javascript and I have a problem. I cannot get my pages and I don't know why. Can someone lead me or give me an explanation to this problem, thanks. I have put my index.js and my package.json below. As you can see I have installed nodemon and bootstrap and I'm doing all of these in cmd.
index.js
const path = require('path');
const expressEdge = require('express-edge');
const express = require('express');
const app = new express();
app.use(express.static('public'));
app.use(expressEdge);
app.set('views', __dirname + '/views');
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, './pages/index.html'));
});
app.get('/about', (req, res) => {
res.sendFile(path.resolve(__dirname, './pages/about.html'));
});
app.get('/contact', (req, res) => {
res.sendFile(path.resolve(__dirname, 'pages/contact.html'));
});
app.get('/post', (req, res) => {
res.sendFile(path.resolve(__dirname, 'pages/post.html'));
});
app.listen(3000, () => {
console.log('App listening on port 3000')
});
package.json
{
"name": "projet3web",
"version": "1.0.0",
"description": "Create a blog",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"},
"keywords": [
"blog"],
"author": "Kym lusinchi Vincent Blanc El Hachemi Sabi",
"license": "ISC",
"dependencies": {
"bootstrap": "^4.3.1",
"bootstrap-datepicker": "^1.8.0",
"express": "^4.16.4",
"express-edge": "^1.0.0",
"nodemon": "^1.18.11",
"popper.js": "^1.15.0",
"startbootstrap-clean-blog": "file:startbootstrap-clean-blog",
"tooltip.js": "^1.3.1"
}
}
Upvotes: 3
Views: 1404
Reputation: 6718
When you do app.use(express.static('public'))
, you're serving all files under public
folder as static
files.
Assuming (from your code) your folder structure is
.
+-- index.js
+-- public
| +-- pages
| | +-- index.html
| | +-- about.html
| | ...
The files in your pages
folder can be accessed like: localhost:3000/pages/index.html
Also, you've not defined a path /index.html
, express is rendering the error.
Upvotes: 0
Reputation: 119
check if your route in the res.send is correct. i dont think you need the .html
i dont know how your files tree looks like but if it is standart you wont need the dot in the beggining of the path
anyway i think you should use res.render('/');
Upvotes: 1