GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — fullstack-javascript/code/15-board-web/web.js at master · Shohan494/fullstack-javascript Expand file tree/ Copy pathweb.jsMore file actions 124 lines (119 loc) · 4.07 KB / Copy pathweb.jsFile metadata and controls124 lines (119 loc) · 4.07 KB 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 var http = require('http'), url = require('url'), path = require('path'), fs = require('fs'), port = process.env.PORT || 1337, staticFolder = 'public', 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') 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 { var uri = url.parse(request.url).pathname console.log('Processing URI: ', uri) if (uri == '' || uri == '/') uri = 'index.html' filename = path.join(__dirname, staticFolder, uri) console.log('Processing file: ', filename) try { stats = fs.statSync(filename) } catch (error) { if (error) { console.error(error) response.writeHead(404, { 'Content-Type': 'text/plain'}) response.write('404 Not Found\n') return response.end() } } if(!stats.isFile()) { response.writeHead(404, { 'Content-Type': 'text/plain'}) response.write('404 Not Found\n') return response.end() } else { var file = fs.readFileSync(filename) if (!file) { response.writeHead(500, {'Content-Type': 'text/plain'}) response.write(err + '\n') return response.end() } var extname = path.extname(filename) var contentType = 'text/html' switch (extname) { case '.js': contentType = 'text/javascript' break case '.css': contentType = 'text/css' break case '.json': contentType = 'application/json' break case '.png': contentType = 'image/png' break case '.jpg': case '.jpeg': contentType = 'image/jpg' break case '.wav': contentType = 'audio/wav' break } response.writeHead(200, { 'Content-Type': contentType, 'Content-Length': file.length }) response.end(file) } } }).listen(port) console.log('Static file server running at\n '+ ' => http://localhost:' + port + '/\nCTRL + C to shutdown') }) |
| 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. |