Skip to content
Advertisement

Node.js & Express.js Font Differentiation

I developed examples on Node.js and Express.js arbtrarily. After initiating example.js of each one shown below, I ran into a font differentiation between them. Even I know Express is a framework for Node, I couldn’t find anywhere why typography change though.

Node.js:

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello Worldn');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Express.js:

var express = require('express')
var app = express()
app.get('/', function (req, res) {
  res.send('Hello World!')
})
app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

Output For Node.js:

Node's browser output

Output For Express.js:

Express's browser output

Advertisement

Answer

and here is Express.js version handles the same job

Well, no, not entirely. Your “plain Node” example explicitly sets the content-type to “text/plain”, but you don’t do the same for the Express example, in which case it will default to “text/html”.

If the server tells the browser that the response contains HTML, the browser will apply a default CSS stylesheet, which usually includes a body font (something like Times New Roman).

When you use “text/plain”, most browsers will render the content in a monospaced font.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement