main problem The /nowendpoint should return the current time.
User
let express = require(‘express’);
let app = express();
// Middleware function to add current time to req.time
const addTimeToRequest = (req, res, next) => {
req.time = new Date().toString();
next();
};
// Route with the middleware and final handler
app.get(‘/now’, addTimeToRequest, (req, res) => {
res.json({ time: req.time, currentTime: new Date() });
});
I’ve spoilered it as it’s a correct solution for this step.
However, you’re now missing some other essential code, without which your app will crash. For instance, app is no longer defined. Also you need to export the app module, so that it can be used by other files in the app.
EDIT: I see that you’re still making changes. You don’t need to start the server on myApp.js as this is handled in server.js. You do need to replace the missing line of code from the bottom of the myApp.js file however:
let express = require('express');
let app = express();
// Middleware function to add current time to req.time
const addTimeToRequest = (req, res, next) => {
req.time = new Date("23 december 2023 9:45:45").toString();
next();
};
// Route with the middleware and final handler
app.get("/now", function(req, res, next) {
req.time = new Date().toString() // Hypothetical synchronous operation
next();
}, function(req, res) {
res.send({time: req.time}); // res.send()
})
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
module.exports = app;
When you post code on the forum, you need to enclose it within two sets of triple backticks, so that it displays correctly. I have edited your most recent reply to demonstrate this.
As for your code, it should actually pass now. I forked your current repl and it passes for me. However, it is rather over-complicated.
Why do you have this for instance? You’ve created a constant here which you then don’t use anywhere.
// Middleware function to add current time to req.time
const addTimeToRequest = (req, res, next) => {
req.time = new Date("23 december 2023 9:45:45").toString();
next();
};
Also, as I said in my previous response, you don’t need this here either, as it’s handled in server.js already:
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
i am a 100% new to this so sorry
this is were i am still stuck
const express = require(‘express’);
const app = express();
// Middleware function to add current time to req.time
const addTimeToRequest = (req, res, next) => {
req.time = new Date(‘28 Decebmer 2023 09:27:48’ ).toString();
next();
};
// Route with the middleware and final handler
app.get(‘/now’, addTimeToRequest, (req, res) => {
res.json({ time: req.time });
});
It’s not actually necessary to create a separate middleware function to generate a datestamp for req.time, though you can do so if you want. The spoilered code in one of my previous posts (which was in your repl at one point) actually works perfectly.
The issue now is that you’re trying to add a date string to your Date object, which is not only mispelled but also not the correct way to generate a current date/time stamp.
If you want to generate a new Date object with the current date and time, you don’t supply an argument to it.
EXAMPLE:
console.log(new Date());
// Thu Dec 28 2023 18:01:06 GMT+0000 (Greenwich Mean Time)
In short, if you empty the parentheses on your Date object above, your code should actually pass!
It would be useful to read up on the Javascript Date object: