So, its been a hot minute since I've used node, and i can not for the life of me, understand why this isn't working.
const body_parser = require("body-parser");
var express = require("express");
var app = express();
app.use("/", express.static(__dirname + "/Contents/"));
app.get("/", function(req, res) {
//Why wont this log to my terminal when a user visits the site?
console.log("log it pleaseeeeeeeeee");
});
app.listen(5004, () => {
console.log("server up and listening on port 5004");
});
I'm trying to log "Log it Pleaseeeee" every time a user visits the site, into the terminal where my nodejs app is running. Why wont this work?
You can't have 2 seperate handlers for an endpoint, in your case "/"
To achieve what you want, you must provide a middleware function.
express
will know based on the type of the second argument what to do. middleware functions expect 3 arguments; the last being a callback so it knows when you are ready.
You should change your code by moving your get function into your app.use('/', ...)
function and including the callback parameter as follows:
const body_parser = require("body-parser");
var express = require("express");
var app = express();
app.use("/", function(req, res, callback) {
console.log("log it pleaseeeeeeeeee");
callback()
}, express.static(__dirname + "/Contents/"));
/** GET RID OF THIS
app.get("/", function(req, res) {
//Why wont this log to my terminal when a user visits the site?
console.log("log it pleaseeeeeeeeee");
});
*/
app.listen(5004, () => {
console.log("server up and listening on port 5004");
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.