Custom created Middleware not functioning

Both log and auth Middleware are not functioning as expected. Running the program using log produces a TypeError: app.use() requires a middleware function. Running the program with auth produces neither an error or the expected result. Without the custom created Middleware the program runs as expected.

Thank you for advice.

index.js:

const morgan = require(‘morgan’);
const helmet = require(‘helmet’);
const Joi = require(‘joi’);
const log = require(’./logger’);
const aut = require(’./auth’);
const express = require(‘express’);
const app = express();

app.use(express.json());
app.use(helmet());
app.use(morgan(‘tiny’));

app.use(log); // Deleting this line removes an error message.
app.use(aut); // This line doesn’t produce anything.

const courses = [
{ id: 1, name: ‘course1’ },
{ id: 2, name: ‘course2’ },
{ id: 3, name: ‘course3’ },
];

app.get(’/’, (req, res) => {
res.send(‘Hello World’);
});

app.get(’/api/courses’, (req, res) => {
res.send(courses);
});

app.post(’/api/courses’, (req, res) => {
const { error } = validateCourse(req.body);
if (error) return res.status(400).send(error.details[0].message);

const course = {
    id: courses.length + 1,
    name: req.body.name
};
courses.push(course);
res.send(course);

});

app.put(’/api/courses/:id’, (req, res) => {

const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send('The course with the given ID was not found.')

const { error } = validateCourse(req.body);
if (error) {
    res.status(400).send(error.details[0].message);
    return;
}

course.name = req.body.name;
res.send(course);

});

function validateCourse(course) {
const schema = Joi.object ({
name: Joi.string().min(3).required()
});
return schema.validate(course);
}

app.get(’/api/courses/:id’, (req, res) => {
const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send(‘The course with the given ID was not found.’)
res.send(course);
});

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(Listening on port ${port}...));

app.delete(’/api/courses/:id’, (req, res) => {

const course = courses.find(c => c.id === parseInt(req.params.id));
if (!course) res.status(404).send('The course with the given ID was not found.')

const index = courses.indexOf(course);
courses.splice(index, 1);

res.send(course);

});

logger.js:

function log(req, res, next) {
console.log(‘Logging…’);
next();
}

module.exports = log;

auth.js:

function auth(req, res, next) {
console.log(‘Authenticating…’);
next();
}

module.exports = auth;

I wonder if your log and aut are being imported correctly. Do you see anything interesting in the console when you log them?

console.log('log:', log);
console.log('aut:', aut);

Thank you for the idea. Terminal recognises them as functions and on the live server the files have been written the same as in the text editor, so the method didn’t reveal any faults in the importation.