Reputation: 15
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
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')
});
Upvotes: 0
Views: 194
Reputation: 24555
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.
Upvotes: 1