@rpichioli/

Express routes, responses and interceptor

Nodejs

Proof of concept application to exemplify some NodeJS stuff and how it works.

fork
loading
Files
  • index.js
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9799146021014931","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgZXhwcmVzcyA9IHJlcXVpcmUoJ2V4cHJlc3MnKTsKY29uc3QgYXBwID0gZXhwcmVzcygpOwoKLy8gU2VuZGluZyBjb250ZW50IHRvIHJlc3BvbnNlIGF0IHJvb3QKYXBwLmdldCgnLycsIChyZXEsIHJlcykgPT4gcmVzLnNlbmQoJ0dFVCByZXF1ZXN0IGludG8gcm9vdC4nKSk7CgovLyBTZW5kaW5nIEpTT04gcmVzcG9uc2UgdW5kZXIgQVBJIHJvdXRlCmFwcC5nZXQoJy9hcGkvJywgKHJlcSwgcmVzKSA9PiByZXMuanNvbih7ZGF0YTogewoJdGl0bGU6ICdEYXRhIEFQSSByZXR1cm5pbmcgSlNPTicsCglsaXN0OiBbOSw3LDYsMywxXSwKCWRhdGV0aW1lOiBuZXcgRGF0ZSgpLnRvTG9jYWxlRGF0ZVN0cmluZygpCn19KSk7CgovLyBBbnkgb3RoZXIgcm91dGUgaW50ZXJjZXB0ZWQgaGVyZQphcHAudXNlKChyZXEsIHJlcykgPT4gcmVzLnNlbmQoJ0FueSBvdGhlciBwYXRoIC0gRGVuaWVkIScpKTsKCmFwcC5saXN0ZW4oMzAwMCwoKSA9PiBjb25zb2xlLmxvZygnU2VydmVyIGxpc3RlbmluZyB0byAzMDAwJykpOw=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const express = require('express');
const app = express();

// Sending content to response at root
app.get('/', (req, res) => res.send('GET request into root.'));

// Sending JSON response under API route
app.get('/api/', (req, res) => res.json({data: {
	title: 'Data API returning JSON',
	list: [9,7,6,3,1],
	datetime: new Date().toLocaleDateString()
}}));

// Any other route intercepted here
app.use((req, res) => res.send('Any other path - Denied!'));

app.listen(3000,() => console.log('Server listening to 3000'));
node v10.16.0