0

I am new to node.js i am following blog and practicing how to create a new blog

https://vegibit.com/node-js-blog-tutorial/

Till this steps everything works fine "Edge Template Engine With Express" , once i added the below lines

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

I am getting the below error.

C:\Automation\NodeJS_Projects-master\nodejs-blog-tutorial\node_modules\express\lib\application.js:210 throw new TypeError('app.use() requires a middleware function') ^

TypeError: app.use() requires a middleware function at Function.use (C:\Automation\NodeJS_Projects-master\nodejs-blog-tutorial\node_modules\express\lib\application.js:210:11) at Object. (C:\Automation\NodeJS_Projects-master\nodejs-blog-tutorial\index.js:8:5) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47

Full code tried to far

    const express = require('express');
    var slash = require("slash");
    const expressEdge = require('express-edge');
    const app = new express();
    var dirname = __dirname;
    if (process.platform === 'win32') dirname = slash(dirname);
    app.use(express.static('public'));
    app.use(expressEdge);
    app.set('views', dirname + '/views');
    
    mongoose.connect('mongodb://localhost:27017/node-blog', { useNewUrlParser: true })
        .then(() => 'You are now connected to Mongo!')
        .catch(err => console.error('Something went wrong', err))
        
    app.get('/posts/new', (req, res) => {
        res.render('create')
    });
     
    app.get('/', (req, res) => {
        res.render('index');
    });
    
    app.get('/about', (req, res) => {
        global.__base = dirname + '/pages/about.html';
        console.log(global.__base)
        res.sendFile(global.__base);
    });
    
    app.get('/contact', (req, res) => {
        global.__base = dirname + '/pages/contact.html';
        res.sendFile(global.__base);
    });
     
    app.get('/post', (req, res) => {
        global.__base = dirname + '/pages/post.html';
        res.sendFile(global.__base);
    });
    
    app.listen(4000, () => {
        console.log('App listening on port 4000')
    });
4
  • I am trying the above code in windows environment Commented Jul 11, 2020 at 19:44
  • What is the error message? Commented Jul 11, 2020 at 19:45
  • C:\Automation\NodeJS_Projects-master\nodejs-blog-tutorial\node_modules\express\lib\application.js:210 throw new TypeError('app.use() requires a middleware function') ^ TypeError: app.use() requires a middleware function at Function.use (C:\Automation\NodeJS_Projects-master\nodejs-blog-tutorial\node_modules\express\lib\application.js:210:11) at Object.<anonymous> (C:\Automation\NodeJS_Projects-master\nodejs-blog-tutorial\index.js:8:5) at Module._compile (internal/modules/cjs/loader.js:1138:30) Commented Jul 11, 2020 at 19:48
  • { "name": "nodejs-blog-tutorial", "version": "1.0.0", "description": "create a blog using node.js", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "blog" ], "author": "", "license": "ISC", "dependencies": { "express": "^4.17.1", "express-edge": "^2.0.2", "mongoose": "^5.9.23", "nodemon": "^2.0.4", "slash": "^3.0.0", "startbootstrap-clean-blog": "^5.0.9" } } Commented Jul 11, 2020 at 19:50

1 Answer 1

1

This tutorial might be outdated as they seem to use an older version of express-edge, since you should set up express-edge as follows (see https://github.com/ecrmnn/express-edge#usage for more details):

const { engine } = require('express-edge');
...
app.use(engine); // instead of app.use(expressEdge);

Alternatively you can do:

const expressEdge = require('express-edge');
...
app.use(expressEdge.engine);

If you take a look at the source, you can see that the module exports an object (containing config and engine) and no middleware, hence the error.

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

3 Comments

Thanks a lot for your pointing out the right error and also pointing the solution
Happy to help :) Please accept the answer if it solved the problem, thank you.
@MageshBalasubramaniam Alternatively you can modify your existing code and do app.use(expressEdge.engine)

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.