0
votes

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')
    });
1
I am trying the above code in windows environmentMagesh Balasubramaniam
What is the error message?Take-Some-Bytes
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)Magesh Balasubramaniam
{ "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" } }Magesh Balasubramaniam

1 Answers

1
votes

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.