Shortest Express apps

Why couldn’t you just run it normally with express? Is there an advantage to running to through http?

4 Likes

I’m not sure, I have been doing it with http all the time. All tutorials I found were using that. Maybe, there’s a shorter way, but this way always worked for me.

1 Like

Oh, yeah, there’s a shorter way, that works as well:

const express = require('express'); // Import the express library
const app = express(); // Launch the express app

/** Replying to request at '/' */
app.get('/', (req, res) => {
  res.send('Testing...'); 
});

app.listen(3000, () => { }); // Opening the 3000 port
2 Likes

btw, isn’t app.listen(3000); enough?

2 Likes

heres smth shorter:

require('express')().listen(9)

you don’t need to define any routes, just let express handle it with a nice 404.

3 Likes

Http is a built in package, whereas Express isn’t. But I don’t necessarily see any ‘advantage’ with that.

3 Likes

Here’s the shortest express app that makes an actual website:

var a=require('express')();a.get('/',(q,r)=>{r.send('');});a.listen(9);
2 Likes

Why use var and not const?

3 Likes

It’s shorter code I suppose, even if it’s bad practice.

3 Likes

what about let? i think var should be phased out (unless you are looking for global vars).

4 Likes

Actually, this is shorter (but worse):

with(require('express')()){use((q,r)=>r.send(''));listen(9)}
4 Likes

Would just a blank r.send() not work?

4 Likes

No, it’d work. You don’t need a port either.

with(require('express')()){use((q,r)=>r.send());listen()}
7 Likes