0

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:

  1. res.sendFile('index');
  2. res.sendFile(path.join(__dirname, 'index.html')
  3. 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}`);
});
6
  • 1
    Is it possible one of your error handlers calls res.render? Commented Dec 22, 2017 at 23:20
  • I doubt this is the problem but...try res.sendFile(path.join(__dirname + '/../public/index.html')). Also, console log in the router.get('/') just to see if you get a response. Commented Dec 22, 2017 at 23:32
  • @ReyHaynes. This worked! I had to require path on the index.js file for it to work, but it worked!. What is the difference between what I had and why would it only accept that syntax...? Commented Dec 22, 2017 at 23:43
  • 1
    @Josh...Good question! There is actually res.render on some of the errorHandlers in my file. I would imagine that would cause me to run into issues when I actually do have errors. So I will correct them and update them. But it didn't seem as though that was what was breaking my code. Thanks for your input though! Commented Dec 22, 2017 at 23:45
  • Glad it worked. I'll pass this as the answer below! To answer the question though, when you have to traverse backward a folder, you need to add a starting forward slash followed by pointing backwards (ie. /..). Commented Dec 23, 2017 at 2:27

2 Answers 2

2

Passing this as the answer

Try res.sendFile(path.join(__dirname + '/../public/index.html'))

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.