Skip to content
This repository was archived by the owner on Jun 22, 2024. It is now read-only.

Commit 6ef5b38

Browse files
express: initial commit
1 parent 82e3c39 commit 6ef5b38

17 files changed

Lines changed: 362 additions & 0 deletions

File tree

servers/express/basics/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Node.js Examples: express
2+
3+
This directory contains some non-trivial examples for express. Express is an exemplary web server framework that implements a simple and powerful backend API interface. According to [their own website](https://expressjs.com/), express is a fast, unopinionated, minimalist web framework for Node.js.
4+
5+
## References:
6+
- npm: https://www.npmjs.com/package/express
7+
- GitHub: https://github.com/expressjs/express
8+
- website: https://expressjs.com/
9+
10+
11+
## Usage:
12+
13+
`npm install` to install as a local project
14+
`node index.js` to run as a standalone server
15+
`npm test` to run the tests
16+
17+
## Contributors ✨
18+
19+
Gireesh Punathil <gpunathi@in.ibm.com>
20+
21+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
22+
23+
Contribution opportunities:
24+
- adding a couple of more common scenarios
25+
- tightening and widening the tests

servers/express/basics/app.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// An express example project to showcase few abstractions
2+
// General layout is:
3+
// - source-in the modules that implement individual functions
4+
// - create an express server app
5+
// - initialize each modules and setup their routes
6+
// - listen for clients
7+
8+
// main express module
9+
var express = require('express')
10+
11+
// implements response redirect
12+
const redirect = require('./redirect.js')
13+
14+
// implements session feature
15+
const session = require('./session.js')
16+
17+
// implements a file upload function
18+
const upload = require('./upload.js')
19+
20+
// a simple time retrieve function
21+
const time = require('./time.js')
22+
23+
// a simple db operation
24+
const db = require('./db.js')
25+
26+
// a default page that lists valid routes
27+
const defaults = require('./defaults.js')
28+
29+
// create the express app
30+
var app = express()
31+
32+
var server
33+
34+
function start() {
35+
// initialize the modules
36+
redirect.setup(app)
37+
session.setup(app)
38+
upload.setup(app, express)
39+
time.setup(app)
40+
db.setup(app)
41+
defaults.setup(app)
42+
43+
// listen for clients!
44+
server = app.listen(12000, () => {
45+
console.log('waiting for client connections!')
46+
console.log('access \'http://localhost:12000\' to get started!')
47+
})
48+
}
49+
50+
function stop() {
51+
db.dbstop()
52+
server.close()
53+
}
54+
55+
exports.stop = stop
56+
exports.start = start

servers/express/basics/db.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// An example that shows a database operation.
2+
// I used an in-memory mongodb server for this
3+
// extract the client address, compute the current
4+
// time and store these in the db. Respond back
5+
// with the history of all client accesses.
6+
7+
// server
8+
9+
// source-in an in-memory server module
10+
const { MongoMemoryServer } = require('mongodb-memory-server')
11+
12+
// server host
13+
const HOST = 'localhost'
14+
15+
// server port
16+
const PORT = 13000
17+
18+
// db instance name
19+
const DBNAME = 'mydb'
20+
21+
// server instance, created through dbstart
22+
let mongod = null
23+
24+
// start a server instance
25+
async function dbstart() {
26+
mongod = new MongoMemoryServer({instance: {port: PORT, dbName: DBNAME}})
27+
await mongod.getDbName()
28+
}
29+
30+
// stop the server instance
31+
exports.dbstop = async function() {
32+
if(mongod)
33+
await mongod.stop()
34+
}
35+
36+
// client
37+
38+
async function exec(req, res) {
39+
// source-in the db client
40+
const MongoClient = require('mongodb').MongoClient
41+
42+
// start the db server
43+
await dbstart()
44+
const url = `mongodb://${HOST}:${PORT}`
45+
46+
// connect to the db server
47+
MongoClient.connect(url, (err, client) => {
48+
const db = client.db(DBNAME)
49+
const collection = db.collection('documents')
50+
51+
// create a new record
52+
var record = {}
53+
record.time= new Date().toString()
54+
record.client = req.connection.remoteAddress
55+
56+
// insert the record into the db
57+
collection.insertMany([record], function(err, result) {
58+
59+
// retrieve all the records back
60+
collection.find({}).toArray(function(err, docs) {
61+
62+
// send it as the response.
63+
res.end(JSON.stringify(docs))
64+
client.close()
65+
})
66+
})
67+
})
68+
}
69+
70+
exports.setup = function(app) {
71+
app.get('/db', (req, res) => {
72+
exec(req, res)
73+
})
74+
}
75+

servers/express/basics/defaults.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
exports.setup = function(app) {
2+
app.get('/', (req, res) => {
3+
res.write('welcome to express examples! the options are:\n\n')
4+
res.write('/time : yields the current server time\n')
5+
res.write('/session : tracks the client visit count, with an expiry\n')
6+
res.write('/upload : a single file upload function\n')
7+
res.write('/db : a simple db example, with an in-memory mongodb\n')
8+
res.end('/redirect : forwards to /landing, a simple demonstration of redirect function\n')
9+
})
10+
}

servers/express/basics/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Entry point of the app when you ran as a server
2+
3+
var app = require('./app.js')
4+
app.start()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "express-basics",
3+
"version": "1.0.0",
4+
"description": "Express basic function examples",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"test": "tap test/*.js"
11+
},
12+
"keywords": [
13+
"express",
14+
"examples",
15+
"nodejs"
16+
],
17+
"author": "gpunathi@in.ibm.com",
18+
"license": "Apache-2.0",
19+
"devDependences": {
20+
"tap": "^14.10.7"
21+
},
22+
"dependencies": {
23+
"express": "^4.17.1",
24+
"express-session": "^1.17.1",
25+
"mongodb-memory-server": "^6.6.1",
26+
"multer": "^1.4.2"
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<body>
3+
<form action="/upload" method="post" enctype="multipart/form-data">
4+
Select file to upload:
5+
<input type="file" name="upload" id="upload">
6+
<input type="submit" value="upload file" name="submit">
7+
</form>
8+
</body>
9+
</html>
10+

servers/express/basics/redirect.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// An example that demonstrates
2+
// response redirect functionality
3+
// It redirects requests to /redirect, to /landing
4+
5+
exports.setup = function(app) {
6+
app.get('/redirect', (req, res) => {
7+
res.redirect('/landing')
8+
})
9+
10+
app.get('/landing', (req, res) => {
11+
res.end('I am a redirect of /redirect')
12+
})
13+
}
14+

servers/express/basics/session.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// An example that demonstrates
2+
// express session functionality
3+
// A session is created with expiry of 10 seconds
4+
// and attached with the express app
5+
6+
var session = require('express-session')
7+
8+
exports.setup = function(app) {
9+
app.use(session({ secret: 'random secret', cookie: { maxAge: 10000}}))
10+
11+
app.get('/session', (req, res) => {
12+
if (!req.session.views) {
13+
req.session.views = 1
14+
} else {
15+
req.session.views++
16+
}
17+
if (req.session.views === 1)
18+
res.end(`thanks for visiting ${req.session.views}st time! (session expires in 10 seconds)`)
19+
else
20+
res.end(`thanks for visiting ${req.session.views}th time! (session expires in 10 seconds)`)
21+
})
22+
}

servers/express/basics/test/db.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const request = require('request')
2+
const server = require('../app.js')
3+
const tap = require('tap')
4+
5+
// start the server
6+
server.start()
7+
8+
// test the /db route
9+
const url = 'http://localhost:12000/db'
10+
request(url, (err, res, body) => {
11+
if(err)
12+
tap.fail(err)
13+
else
14+
tap.match(body, /client/, 'db record has keyword "client" in it')
15+
server.stop()
16+
})
17+

0 commit comments

Comments
 (0)