Jerlam
Jerlam

Reputation: 1081

Express - Error: No default engine was specified and no extension was provided

I cannot get my api to work with my express server, I always get an error:

Error: No default engine was specified and no extension was provided.

I am pretty sure it is coming from express router but I cannot find where the error comes from...

Did I use express router correctly? Because I have never used it before.

server.js:

const createError = require('http-errors');
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const api = require('./routes/api');
const app = express();
const MongoClient = require('mongodb').MongoClient;

const MONGODB_URI = process.env.MONGODB_URI;
const PORT = process.env.PORT || 5000;

app.use('/', api);

app.use(logger('dev'));

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

// Database object
var db;

// Initialize connection once with db
MongoClient.connect(MONGODB_URI, function(err, database) {
    if(err) throw err;

    db = database;

    // Start the application after the database connection is ready
    app.listen(PORT, () => console.log(`Server listening on port ${PORT}`));
});

module.exports = db;
module.exports = app;

routes/api.js:

// const app = require("../server");
var db = require("../server");
const { videos } = require("../helpers/videos");
const { videoLinksValidator } = require("../helpers/video-links-validator");
const { getTitleType } = require("../helpers/title-type-extractor");
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    console.log("RESPONSE", res) 
});

// Create a GET route
router.get("/express_backend", (req, res) => {
    res.send({ express: "YOUR EXPRESS BACKEND IS CONNECTED TO REACT" });
});

router.post("/video_url", async (req, res)=> {
    const videoURL = req.body.videoURL.videoURL;

    let titleType = await getTitleType(videoURL);

    let videosAndTitle = await videos(videoURL, titleType);

    let videoAndTitleReady = await videoLinksValidator(videosAndTitle);
    console.log(videoAndTitleReady)
    return res.send(videoAndTitleReady);
});

module.exports = router;

Upvotes: 8

Views: 32980

Answers (3)

agelgel
agelgel

Reputation: 51

I had the same error. I forgot to add the syntax below to the server.js file

//set default engine, and provide [handlebars as] extension
app.set('view engine', 'handlebars');

Upvotes: 5

ANIK ISLAM SHOJIB
ANIK ISLAM SHOJIB

Reputation: 3238

In your code, you are rendering while if your view engine does not functional then you are going to end up with an error

   res.status(err.status || 500);
    res.render('error');

So if you are using your application as API gateway then you must send those responses. As res.send currently deprecated use

  res.status(err.status || 500).send('error');

Upvotes: 0

Rajan Lagah
Rajan Lagah

Reputation: 2528

This error is because you are using res.render. res.render will try to render the page for that you have to set view engine like jade etc. But here you do not need that so change res.render to res.send like this
Try this

app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.send('error');//this or res.status(err.status || 500).send('error')
});

For your second question you have to bring bodyParser at the top like this in server.js

app.use(bodyParser.json()); //here
app.use('/', api);

app.use(logger('dev'));

//app.use(bodyParser.json()); your old code

Upvotes: 13

Related Questions