web application with express code example
Example 1: node.js express
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Example 2: node js express
const express = require('express');
const path = require('path');
const PORT = process.env.PORT || 5000;
const app = express();
function error404(req, res) {
res.status(404);
if (req.accepts('html')) {
res.sendFile(path.join(__dirname, 'public/errors/404.html'));
return;
}
if (req.accepts('json')) {
res.send({
status: 404,
error: 'Not found'
});
return;
}
res.type('txt').send('404 - Not found');
}
app
.use(express.static(path.join(__dirname, 'public')))
.use(error404)
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
Example 3: express js
Express.js, or simply Express, is a web application framework for Node.js,
released as free and open-source software under the MIT License.
It is designed for building web applications and APIs.
It has been called the de facto standard server framework for Node.js.