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

express examples #21

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/servers-express-basics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: servers-express-basics

on:
push:
paths:
- 'servers/express/basics/**'
pull_request:
paths:
- 'servers/express/basics/**'
schedule:
# Every day at 1am
- cron: '0 1 * * *'

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
node-version: [12.x, 14.x]
env:
CI: true
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install
run: npm install
working-directory: './servers/express/basics'
- name: npm install tap
run: npm install --save-dev tap
working-directory: './servers/express/basics'
- name: npm run test
run: npm run test
working-directory: './servers/express/basics'
9 changes: 7 additions & 2 deletions servers/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Node.js Examples: Servers

In this directory, we have examples of servers that are built with Node.js. See the [Examples section](../README.md#examples) of the root [README.md](../README.md) for a list of all projects contained within, or take a look through the directories yourself.
In this directory, we have examples of servers that are built with Node.js.
See the [Examples section](../README.md#examples) of the root
[README.md](../README.md) for a list of all projects contained within,
or take a look through the directories yourself.

## What is a Server in the context of nodejs/examples

Expand All @@ -11,4 +14,6 @@ Any software built with Node.js that serves content over HTTP. For example:

## Contributing to Examples

If you're interested in seeing more examples of servers using Node.js or want to help maintain the current examples, we more than welcome your contribution! See [CONTRIBUTING.md](../CONTRIBUTING.md) for more details 🤗
If you're interested in seeing more examples of servers using Node.js
or want to help maintain the current examples, we more than welcome
your contribution! See [CONTRIBUTING.md](../CONTRIBUTING.md) for more details 🤗
34 changes: 34 additions & 0 deletions servers/express/basics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Node.js Examples: express

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.

## References

- [npm:](https://www.npmjs.com/package/express)

- [GitHub:](https://github.com/expressjs/express)

- [website:](https://expressjs.com)

## Usage

`npm install` to install as a local project
`node index.js` to run as a standalone server
`npm test` to run the tests

## Contributors ✨

Gireesh Punathil <[email protected]>

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors)
specification. Contributions of any kind welcome!

Contribution opportunities

- adding a couple of more common scenarios

- tightening and widening the tests
56 changes: 56 additions & 0 deletions servers/express/basics/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// An express example project to showcase few abstractions
// General layout is:
// - source-in the modules that implement individual functions
// - create an express server app
// - initialize each modules and setup their routes
// - listen for clients

// main express module
var express = require('express')

// implements response redirect
const redirect = require('./redirect.js')

// implements session feature
const session = require('./session.js')

// implements a file upload function
const upload = require('./upload.js')

// a simple time retrieve function
const time = require('./time.js')

// a simple db operation
const db = require('./db.js')

// a default page that lists valid routes
const defaults = require('./defaults.js')

// create the express app
var app = express()

var server

function start() {
// initialize the modules
redirect.setup(app)
session.setup(app)
upload.setup(app, express)
time.setup(app)
db.setup(app)
defaults.setup(app)

// listen for clients!
server = app.listen(12000, () => {
console.log('waiting for client connections!')
console.log('access \'http://localhost:12000\' to get started!')
})
}

function stop() {
db.dbstop()
server.close()
}

exports.stop = stop
exports.start = start
75 changes: 75 additions & 0 deletions servers/express/basics/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// An example that shows a database operation.
// I used an in-memory mongodb server for this
// extract the client address, compute the current
// time and store these in the db. Respond back
// with the history of all client accesses.

// server

// source-in an in-memory server module
const { MongoMemoryServer } = require('mongodb-memory-server')

// server host
const HOST = 'localhost'

// server port
const PORT = 13000

// db instance name
const DBNAME = 'mydb'

// server instance, created through dbstart
let mongod = null

// start a server instance
async function dbstart() {
mongod = new MongoMemoryServer({instance: {port: PORT, dbName: DBNAME}})
await mongod.getDbName()
}

// stop the server instance
exports.dbstop = async function() {
if(mongod)
await mongod.stop()
}

// client

async function exec(req, res) {
// source-in the db client
const MongoClient = require('mongodb').MongoClient

// start the db server
await dbstart()
const url = `mongodb://${HOST}:${PORT}`

// connect to the db server
MongoClient.connect(url, (err, client) => {
const db = client.db(DBNAME)
const collection = db.collection('documents')

// create a new record
var record = {}
record.time= new Date().toString()
record.client = req.connection.remoteAddress

// insert the record into the db
collection.insertMany([record], function(err, result) {

// retrieve all the records back
collection.find({}).toArray(function(err, docs) {

// send it as the response.
res.end(JSON.stringify(docs))
client.close()
})
})
})
}

exports.setup = function(app) {
app.get('/db', (req, res) => {
exec(req, res)
})
}

10 changes: 10 additions & 0 deletions servers/express/basics/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports.setup = function(app) {
app.get('/', (req, res) => {
res.write('welcome to express examples! the options are:\n\n')
res.write('/time : yields the current server time\n')
res.write('/session : tracks the client visit count, with an expiry\n')
res.write('/upload : a single file upload function\n')
res.write('/db : a simple db example, with an in-memory mongodb\n')
res.end('/redirect : forwards to /landing, a simple demonstration of redirect function\n')
})
}
4 changes: 4 additions & 0 deletions servers/express/basics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Entry point of the app when you ran as a server

var app = require('./app.js')
app.start()
28 changes: 28 additions & 0 deletions servers/express/basics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "express-basics",
"version": "1.0.0",
"description": "Express basic function examples",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "tap test/*.js"
},
"keywords": [
"express",
"examples",
"nodejs"
],
"author": "[email protected]",
"license": "Apache-2.0",
"devDependences": {
"tap": "^14.10.7"
},
"dependencies": {
"express": "^4.17.1",
"express-session": "^1.17.1",
"mongodb-memory-server": "^6.6.1",
"multer": "^1.4.2"
}
}
10 changes: 10 additions & 0 deletions servers/express/basics/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="submit" value="upload file" name="submit">
</form>
</body>
</html>

14 changes: 14 additions & 0 deletions servers/express/basics/redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// An example that demonstrates
// response redirect functionality
// It redirects requests to /redirect, to /landing

exports.setup = function(app) {
app.get('/redirect', (req, res) => {
res.redirect('/landing')
})

app.get('/landing', (req, res) => {
res.end('I am a redirect of /redirect')
})
}

22 changes: 22 additions & 0 deletions servers/express/basics/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// An example that demonstrates
// express session functionality
// A session is created with expiry of 10 seconds
// and attached with the express app

var session = require('express-session')

exports.setup = function(app) {
app.use(session({ secret: 'random secret', cookie: { maxAge: 10000}}))

app.get('/session', (req, res) => {
if (!req.session.views) {
req.session.views = 1
} else {
req.session.views++
}
if (req.session.views === 1)
res.end(`thanks for visiting ${req.session.views}st time! (session expires in 10 seconds)`)
else
res.end(`thanks for visiting ${req.session.views}th time! (session expires in 10 seconds)`)
})
}
17 changes: 17 additions & 0 deletions servers/express/basics/test/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const request = require('request')
const server = require('../app.js')
const tap = require('tap')

// start the server
server.start()

// test the /db route
const url = 'http://localhost:12000/db'
request(url, (err, res, body) => {
if(err)
tap.fail(err)
else
tap.match(body, /client/, 'db record has keyword "client" in it')
server.stop()
})

18 changes: 18 additions & 0 deletions servers/express/basics/test/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const request = require('request')
const server = require('../app.js')
const tap = require('tap')

// start the server
server.start()

// test the / route
const url = 'http://localhost:12000'
request(url, (err, res, body) => {
if(err)
tap.fail(err)
else
tap.match(body, /examples/, 'response string has keyword "examples" in it')
server.stop()
})


18 changes: 18 additions & 0 deletions servers/express/basics/test/forward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const request = require('request')
const server = require('../app.js')
const tap = require('tap')

// start the server
server.start()

// test the /redirect route
const url = 'http://localhost:12000/redirect'
request(url, (err, res, body) => {
if(err)
tap.fail(err)
else
tap.match(body, /redirect/, 'response string has keyword "redirect" in it')
server.stop()
})


18 changes: 18 additions & 0 deletions servers/express/basics/test/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const request = require('request')
const server = require('../app.js')
const tap = require('tap')

// start the server
server.start()

// test the /session route
const url = 'http://localhost:12000/session'
request(url, (err, res, body) => {
if(err)
tap.fail(err)
else
tap.match(body, /visit/, 'response string has keyword "visit" in it')
server.stop()
})


Loading