Reputation: 421
I have tried looking through all of the questions resolved by StackOverflow regarding people receiving this error:
Error: No default engine was specified and no extension was provided.
I feel like I have scoured every post on SO and none of the answers have corrected my code. There must be something else going on.
My issue that I am having is that I already created many html files before making a decision to use Node.js + Express for my back-end...so rather than worry about converting them all to a templating engine like pug or EJS, I just want to serve them from a static public folder in my directory.
From what I have researched, you do not need a templating engine to use Node.js + Express. But you do need to set up a static public folder to serve your files.
I have included the app.use(express.static(path.join(__dirname, 'public')));
in my app.js file. I have added a 'public' folder and put all of my static files inside of it, and in my routes file (my index.js file) I have written the following route:
router.get('/', (req, res) => {
res.sendFile('./public/index.html');
});
I have tried changing it to the following:
res.sendFile('index');
res.sendFile(path.join(__dirname, 'index.html')
res.sendFile('../public/index.html');
I have also tried changing my static middleware syntax to just app.use(express.static('public'));
and that didn't seem to change anything either.
All of these render the No default engine was specified and no extension was provided.
error I provided above. I hate that I need to ask this questions when there are so many of the same questions on StackOverflow, but I am currently completely stumped at to what to do. Without further adieu, here is my code:
This is my file directory structure.
Here is my index.js file handling all my routes:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
router.post('/', (req, res) => {
});
module.exports = router;
Here's my app.js file:
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const routes = require('./routes/index');
const errorHandlers = require('./handlers/errorHandlers');
const app = express();
app.use('/', routes);
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// One of our error handlers will see if these errors are just validation errors
app.use(errorHandlers.flashValidationErrors);
// Otherwise this was a really bad error we didn't expect! Shoot eh
if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
app.use(errorHandlers.developmentErrors);
}
// production error handler
app.use(errorHandlers.productionErrors);
module.exports = app;
Here's my package.json:
{
"name": "********",
"version": "1.0.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-imagemin": "^1.0.1",
"grunt-contrib-sass": "^1.0.0",
"grunt-contrib-uglify": "^2.3.0",
"grunt-contrib-watch": "^1.0.0",
"webpack-dev-server": "^2.9.7"
},
"description": "",
"main": "app.js",
"scripts": {
"start": "nodemon ./start.js"
},
"author": "****** ********",
"license": "ISC",
"dependencies": {
"cookie-parser": "^1.4.3",
"dotenv": "^4.0.0",
"express": "^4.16.2",
"mongod": "^2.0.0",
"mongoose": "^4.13.7",
"nodemon": "^1.14.1",
"normalize.css": "^6.0.0"
}
}
Here's my start.js file that runs when I run npm start
:
const mongoose = require('mongoose');
// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });
mongoose.connect(process.env.DATABASE, { useMongoClient: true });
mongoose.Promise = global.Promise;
mongoose.connection.on('error', (err) => {
console.error(`${err.message}`);
});
const app = require('./app');
app.set('port', process.env.PORT || 7777);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
Upvotes: 0
Views: 2067
Reputation: 1773
If you're starting from scratch, you can run:
npm install -g express-generator # to install express-generator globally
express projectName --no-view --git # '--git' is optional and generates a '.gitignore' file
Upvotes: 1
Reputation: 3102
Passing this as the answer
Try res.sendFile(path.join(__dirname + '/../public/index.html'))
Upvotes: 2