0

Im using edge template engine in my application node.js and I getting back the error; TypeError: app.use() requires middleware function

this is my code the index.js

const path = require('path');
const express = require('express');
const expressEdge = require('express-edge');




const app = new express();

app.use(express.static('public'));
app.use(expressEdge);
app.set('views', __dirname + '/views');


app.get('/', (req, res) => {
  res.render('index');
});
app.get('/about',(req,res)=>{
  res.sendFile(path.resolve(__dirname,'pages/about.html'));
});
app.get('/post',(req,res)=>{
  res.sendFile(path.resolve(__dirname,'pages/post.html'));
});

app.get('/contact',(req,res)=>{
  res.sendFile(path.resolve(__dirname,'pages/contact.html'));
});


app.listen(4000,() =>{
  console.log('App listening on port 4000')
});

This is how I set up the folder in my application enter image description here

Kent Kostelac
  • 2,257
  • 3
  • 29
  • 42
JP8
  • 3
  • 2
  • There are many of questions on SO that regards TypeError with app.use(). Why are they not relevant to you? As far as I can tell app.use() takes 3 arguments and you only give it one.[link1](https://stackoverflow.com/questions/32883626/typeerror-app-use-requires-middleware-functions), [link2](https://stackoverflow.com/questions/31496100/cannot-app-usemulter-requires-middleware-function-error) – Kent Kostelac Feb 20 '20 at 01:42
  • Does this answer your question? [Cannot app.use(multer). "requires middleware function" error](https://stackoverflow.com/questions/31496100/cannot-app-usemulter-requires-middleware-function-error) – Kent Kostelac Feb 20 '20 at 01:42

1 Answers1

0

From the docs, you are supposed to pass expressEdge.engine as the middleware to app.use not the entire expressEdge object. So this is what your index.js code should be like:

const path = require('path');
const express = require('express');
// Use the config method to configure express-edge if needed
const { config, engine } = require('express-edge');

const app = new express();

app.use(express.static('public'));
// Pass in the engine not the entire express-edge object
app.use(engine);
app.set('views', __dirname + '/views');


app.get('/', (req, res) => {
  res.render('index');
});
app.get('/about',(req,res)=>{
  res.sendFile(path.resolve(__dirname,'pages/about.html'));
});
app.get('/post',(req,res)=>{
  res.sendFile(path.resolve(__dirname,'pages/post.html'));
});

app.get('/contact',(req,res)=>{
  res.sendFile(path.resolve(__dirname,'pages/contact.html'));
});


app.listen(4000,() =>{
  console.log('App listening on port 4000')
});

More details in the usage documentation here.

Tunmee
  • 2,483
  • 1
  • 7
  • 13