Why couldn’t you just run it normally with express? Is there an advantage to running to through http
?
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.
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
btw, isn’t app.listen(3000);
enough?
heres smth shorter:
require('express')().listen(9)
you don’t need to define any routes, just let express handle it with a nice 404.
Http is a built in package, whereas Express isn’t. But I don’t necessarily see any ‘advantage’ with that.
Here’s the shortest express app that makes an actual website:
var a=require('express')();a.get('/',(q,r)=>{r.send('');});a.listen(9);
Why use var
and not const
?
It’s shorter code I suppose, even if it’s bad practice.
what about let
? i think var should be phased out (unless you are looking for global vars).
Actually, this is shorter (but worse):
with(require('express')()){use((q,r)=>r.send(''));listen(9)}
Would just a blank r.send()
not work?
No, it’d work. You don’t need a port either.
with(require('express')()){use((q,r)=>r.send());listen()}