Wednesday 24 June 2020

New to ExpressJS. Why does this basic request print twice?

Hi all,I just started learning about Express and middleware. I have the following code:"use strict" var express = require('express'); var app = express(); // allows you to work with file and directory paths var path = require('path'); app.get('/', log, hello); function log(req, res, next) { // THIS ALWAYS GET PRINTED TWICE console.log(new Date(), req.method, req.url); next(); } function hello(req, res) { res.write('Hello'); res.end(); } app.listen(3000, function() { console.log('Web server is listening on port 3000') }) Basically, I have a an app.get() that calls the log and hello functions on the request. Despite the log function only getting called once, it prints the output twice.I've read here (https://stackoverflow.com/questions/54957409/why-does-the-console-log-appear-twice-in-app-js-in-nodejs) that as I am using Chrome, it must be because of a pre-flight CORS - you're doing one request but the browser is doing two so it shows both. Is anyone able to explain what this means exactly and why it happens in Chrome but not Firefox (in Firefox, it only prints once)? I'm on port 3000 - I've read the StackOverflow post but don't really understand why making the request through a browser means you're doing it to a different port.Thank you- any help would be appreciated!

Submitted June 24, 2020 at 02:27PM by definitely-trying

No comments:

Post a Comment