GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fullstack-javascript/code/14-board-api/web.js at master · mdixon47/fullstack-javascript

Expand file tree

/

web.js

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
61 lines (59 loc) · 2.26 KB
/

web.js

Copy path

File metadata and controls

61 lines (59 loc) · 2.26 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var http = require('http')
var util = require('util')
var querystring = require('querystring')
var client = require('mongodb').MongoClient
var uri = process.env.MONGOLAB_URI || 'mongodb://@127.0.0.1:27017/messages'
//MONGOLAB_URI=mongodb://user:[email protected]:port/db_name
client.connect(uri, function(error, db) {
if (error) return console.error(error)
var collection = db.collection('messages')
var app = http.createServer(function (request, response) {
var origin = (request.headers.origin || '*')
if (request.method == 'OPTIONS') {
response.writeHead('204', 'No Content', {
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods':
'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'content-type, accept',
'Access-Control-Max-Age': 10, // In seconds
'Content-Length': 0
})
response.end()
} else if (request.method === 'GET' && request.url === '/messages/list.json') {
collection.find().toArray(function(error,results) {
if (error) return console.error(error)
var body = JSON.stringify(results)
response.writeHead(200,{
'Access-Control-Allow-Origin': origin,
'Content-Type':'text/plain',
'Content-Length':body.length
})
console.log('LIST OF OBJECTS: ')
console.dir(results)
response.end(body)
})
} else if (request.method === 'POST' && request.url === '/messages/create.json') {
request.on('data', function(data) {
console.log('RECEIVED DATA:')
console.log(data.toString('utf-8'))
collection.insert(JSON.parse(data.toString('utf-8')),
{safe:true}, function(error, obj) {
if (error) return console.error(error)
console.log('OBJECT IS SAVED: ')
console.log(JSON.stringify(obj))
var body = JSON.stringify(obj)
response.writeHead(200,{
'Access-Control-Allow-Origin': origin,
'Content-Type':'text/plain',
'Content-Length':body.length
})
response.end(body)
})
})
} else {
response.end('Supported endpoints: \n/messages/list.json\n/messages/create.json')
}
})
var port = process.env.PORT || 1337
app.listen(port)
})
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.