Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
The core part of an Express app is the Application object. It’s the application itself.
In this article, we’ll look at the methods of the app
object and what we can do with it.
Methods
app.disabled(name)
The method returns true
if the given setting is disabled.
For example, if we have:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.disable('foo');
app.get('/', function (req, res, next) {
res.json({ fooDisabled: app.disabled('foo') });
})
app.listen(3000, () => console.log('server started'));
Then we get:
{"fooDisabled":true}
since we called app.disable(‘foo’);
On the other hand, if we called app.enable(‘foo’);
then we get:
{"fooDisabled":false}
app.enable(name)
We can use enable
to set the setting with the given name to true
.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.enable('foo');
app.get('/', function (req, res, next) {
res.json({ fooDisabled: app.disabled('foo') });
})
app.listen(3000, () => console.log('server started'));
Then we get:
{"fooDisabled":false}
since we called enable
with 'foo'
passed in.
app.enabled(name)
The enabled
method returns if a setting with the given name is enabled. For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.enable('foo');
app.get('/', function (req, res, next) {
res.json({ fooDisabled: app.enabled('foo') });
})
app.listen(3000, () => console.log('server started'));
Then we get {“fooDisabled”:true}
since we called app.enable(‘foo’);
.
app.engine(ext, callback)
We call the engine
method to set the template engine that we use to render our HTML output.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('views', './views');
app.get('/', (req, res, next) => {
res.render('index', { people: ['geddy', 'neil', 'alex'] })
})
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.listen(3000, () => console.log('server started'));
Then we can add our template to views/index.ejs
as follows:
<%= people.join(", "); %>
and we get:
geddy, neil, alex
displayed.
The callback
should have the parameters, path
, options
, and callback
.
app.get(name)
We can use the get
method to get the value of a setting with the given name.
For example, if we have:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('title', 'Foo');
app.get('/', (req, res, next) => {
res.send(app.get('title'));
})
app.listen(3000, () => console.log('server started'));
Then we get Foo
displayed since we called:
app.set('title', 'Foo');
app.get(path, callback [, callback …])
The get
method lets us pass in a route handler callback or a series of them to handle get requests with the given path
.
It takes the following arguments:
-
path
— it can be a string or regex representing paths or patterns of paths. The default is/
. -
callback
— a function to handle requests. It can be a middleware function, a series of them, array of them, or a combination of all of the above
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('title', 'Foo')
app.get('/', (req, res, next) => {
res.send('GET request made');
})
app.listen(3000, () => console.log('server started'));
app.listen(path, [callback])
It starts a UNIX socket and listens to connects on the given path.
For example, we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('title', 'Foo')app.get('/', (req, res, next) => {
res.send('hi');
})
app.listen('/tmp/sock');
Then when the /tmp/sock
socket isn’t in use, we can start our app by listening to this socket.
app.listen([port[, host[, backlog]][, callback])
This listen
method listens for connects on the given host and port.
If the port is omitted or it’s 0, then the operating system will assign an arbitrary port for it to listen to.
We can pass the Express app
object to the http.createServer
to listen to connections as follows:
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res, next) => {
res.send('hi');
})
http.createServer(app).listen(3000);
This lets us easily provide both an HTTP and HTTPS version of the app with the same code base.
app
is actually a function so we can pass it into http.createServer
as a callback.
app.listen()
returns an http.Server
object and it’s a convenience method for the following:
app.listen = function () {
const server = http.createServer(this)
return server.listen.apply(server, arguments)
}
So we can use it as follows:
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res, next) => {
res.send('hi');
})
app.listen = function () {
const server = http.createServer(this)
return server.listen.apply(server, arguments)
}
app.listen(3000);
Conclusion
We can check is a setting is set to false
with the disabled
method.
To process GET requests, we can use the get
method. It takes a route path and one or more route handler callbacks.
Finally, we have the listen
method to listen for connections from UNIX sockets or given host and port. app
can be passed into Node.js’ http.createServer
method as a callback function since app
is actually a function.