You should not use var at all. It’s a very old way to declare variables.
That code as written probably doesn’t do what you think. That logic for creating a response shouldn’t be floating around outside of a function, if I recall correctly. What is the link to the step?
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (/workspace/boilerplate-express/node_modules/express/lib/router/index.js:469:13)
at Function.<anonymous> (/workspace/boilerplate-express/node_modules/express/lib/application.js:227:21)
at Array.forEach (<anonymous>)
at Function.use (/workspace/boilerplate-express/node_modules/express/lib/application.js:224:7)
at Object.setupBackgroundApp (/workspace/boilerplate-express/node_modules/fcc-express-bground/index.js:162:9)
at Object.<anonymous> (/workspace/boilerplate-express/server.js:25:9)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Object..js (node:internal/modules/cjs/loader:1689:10)
at Module.load (node:internal/modules/cjs/loader:1318:32)
at Function._load (node:internal/modules/cjs/loader:1128:12)
node:events:496
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1907:16)
at listenInCluster (node:net:1964:12)
at Server.listen (node:net:2066:7)
at Function.listen (/workspace/boilerplate-express/node_modules/express/lib/application.js:635:24)
at Object.<anonymous> (/workspace/boilerplate-express/server.js:25:51)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Object..js (node:internal/modules/cjs/loader:1689:10)
at Module.load (node:internal/modules/cjs/loader:1318:32)
at Function._load (node:internal/modules/cjs/loader:1128:12)
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1943:8)
at process.processTicksAndRejections (node:internal/process/task_queues:90:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
What do they mean?
myApp.js:
// Load environment variables at the very beginning of your app
require('dotenv').config();
const express = require('express');
const app = express();
const path = require('path');
// Step 1
app.get('/', function(req, res) {
res.send("Hello Express");
});
// Step 2
app.get('/another-route', function(req, res) {
let filePath = path.join(__dirname, "/views/index.html");
res.sendFile(filePath);
});
// Step 3
let publicPath = path.join(__dirname, "/public");
app.use("/public", express.static(publicPath));
// Step 4
let helloObj = { "message": "Hello json" };
app.get('/json-default', function(req, res) {
res.json(helloObj);
});
// Step 5 (updated)
app.get('/json', function(req, res) {
// Now using process.env directly, which is populated by dotenv
const mySecret = process.env.MESSAGE_STYLE;
// Call the formatResponse function
const responseMessage = formatResponse(mySecret);
// Respond with the formatted message
res.json({ "message": responseMessage });
});
// Function definition
function formatResponse(letName) {
if (letName === "uppercase") {
return "Hello json".toUpperCase();
} else {
return "Hello json";
}
}
// Start the server
const server = app.listen(3000)
.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log('Port 3000 in use, trying another...');
server.listen(0);
} else {
console.error(err);
}
});
It may be causing more issues
You may want to do the thing of changing the start script so you don’t need to stop and restart the server everytime, if you forget to stop the server, you can’t start it. At this time it looks like you have port 3000 occupied, you need to kill the process you have there