|
| 1 | +/** |
| 2 | + * Chat Server Example |
| 3 | + */ |
| 4 | +const express = require('express'); |
| 5 | +const app = express(); |
| 6 | + |
| 7 | +const port = 8081; |
| 8 | + |
| 9 | +if (app.get('env') === 'production') { |
| 10 | + app.set('trust proxy', 1); // trust first proxy |
| 11 | +} |
| 12 | + |
| 13 | +app.use(express.json()) |
| 14 | +app.use(express.urlencoded({extended: true})); |
| 15 | + |
| 16 | +// set mddielware for CORS |
| 17 | +app.use(function (req, res, next) { |
| 18 | + res.header("Access-Control-Allow-Origin", "*"); |
| 19 | + res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept"); |
| 20 | + next(); |
| 21 | +}); |
| 22 | + |
| 23 | +//provide json api |
| 24 | +app.set('json spaces', 2); |
| 25 | + |
| 26 | +const handleRequest = (req, res, data) => { |
| 27 | + |
| 28 | + const {userInputText, callback} = data; |
| 29 | + |
| 30 | + console.log(`[chat-server] #chat request userInputText:"${userInputText}"`); |
| 31 | + |
| 32 | + //create response object |
| 33 | + const response = { |
| 34 | + output: [] |
| 35 | + }; |
| 36 | + |
| 37 | + const msg = response.output; |
| 38 | + |
| 39 | + if (!userInputText) { |
| 40 | + |
| 41 | + //if inputText is empty |
| 42 | + msg.push({ |
| 43 | + type: "text", |
| 44 | + value: "Hey, please say something!" |
| 45 | + }); |
| 46 | + |
| 47 | + } else { |
| 48 | + |
| 49 | + if (userInputText == "show buttons") { |
| 50 | + |
| 51 | + //show text and buttons |
| 52 | + msg.push({ |
| 53 | + type: "text", |
| 54 | + value: "Ok, I'll show you buttons!", |
| 55 | + delayMs: 500 // wait(milliseconds) |
| 56 | + }); |
| 57 | + |
| 58 | + const opts = []; |
| 59 | + opts.push({label: 'label1', value: 'value1'}); |
| 60 | + opts.push({label: 'label2', value: 'value2'}); |
| 61 | + opts.push({label: 'label3', value: 'value3'}); |
| 62 | + |
| 63 | + msg.push({type: "option", options: opts}); |
| 64 | + |
| 65 | + } else if (userInputText == "show image") { |
| 66 | + |
| 67 | + //show text and image |
| 68 | + msg.push({ |
| 69 | + type: "text", |
| 70 | + value: "Ok, I'll show you an image!", |
| 71 | + delayMs: 500 |
| 72 | + }); |
| 73 | + |
| 74 | + msg.push({ |
| 75 | + type: "image", |
| 76 | + value: " https://upload.wikimedia.org/wikipedia/commons/a/a3/Aptenodytes_forsteri_-Snow_Hill_Island%2C_Antarctica_-adults_and_juvenile-8.jpg" |
| 77 | + }); |
| 78 | + } else if (userInputText == "show link") { |
| 79 | + |
| 80 | + // show html |
| 81 | + msg.push({ |
| 82 | + type: 'html', |
| 83 | + value: 'Click <a href="https://github.com/riversun" target="_blank" >here</a> to open a page.', |
| 84 | + delayMs: 500 |
| 85 | + }); |
| 86 | + |
| 87 | + |
| 88 | + } else if (userInputText == "show double") { |
| 89 | + |
| 90 | + |
| 91 | + msg.push({ |
| 92 | + type: "text", |
| 93 | + value: "Ok, This is the 1st message", |
| 94 | + delayMs: 500 |
| 95 | + }); |
| 96 | + |
| 97 | + |
| 98 | + msg.push({ |
| 99 | + type: "text", |
| 100 | + value: "Ok, This is the 2nd message", |
| 101 | + delayMs: 500 |
| 102 | + }); |
| 103 | + |
| 104 | + |
| 105 | + } else { |
| 106 | + //echo inputText |
| 107 | + msg.push({ |
| 108 | + type: "text", |
| 109 | + value: "You say '" + userInputText + "'" |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (callback) { |
| 115 | + //generate response for JSONP |
| 116 | + const responseText = callback + "(" + JSON.stringify(response) + ")"; |
| 117 | + |
| 118 | + //Set content-type to "application/javascript" |
| 119 | + res.set('Content-Type', 'application/javascript'); |
| 120 | + res.send(responseText) |
| 121 | + |
| 122 | + console.log(`[chat-server] #chat response responseText:"${responseText}"`); |
| 123 | + } else { |
| 124 | + //generate response for JSON |
| 125 | + |
| 126 | + res.json(response); |
| 127 | + |
| 128 | + console.log(`[chat-server] #chat response responseText:"${JSON.stringify(response)}"`); |
| 129 | + } |
| 130 | + |
| 131 | +}; |
| 132 | +app.get('/chat', function (req, res) { |
| 133 | + console.log(`[chat-server] #chat GET /chat requested" query="${JSON.stringify(req.query)}"`); |
| 134 | + |
| 135 | + //Get "text" parameter from request |
| 136 | + const userInputText = req.query.text; |
| 137 | + |
| 138 | + //get callback parameter for jsonp response |
| 139 | + const callback = req.query.callback; |
| 140 | + handleRequest(req, res, {userInputText, callback}); |
| 141 | + |
| 142 | +}); |
| 143 | + |
| 144 | +app.post('/chat', function (req, res) { |
| 145 | + |
| 146 | + //Get "text" parameter from request |
| 147 | + const data = req.body; |
| 148 | + |
| 149 | + |
| 150 | + console.log(`[chat-server] #chat POST /chat requested" data="${JSON.stringify(data)}"`); |
| 151 | + |
| 152 | + |
| 153 | + handleRequest(req, res, {userInputText: data.text}); |
| 154 | + |
| 155 | +}); |
| 156 | +app.listen(port, () => { |
| 157 | + console.log('[chat-server] chat server started on port:' + port); |
| 158 | + console.log(`[chat-server] To use this chat server, |
| 159 | + First start webpack-devserver with "npm start". |
| 160 | + Next, go to "http://localhost:8080/index.html?endpoint=http://localhost:8081/chat" .`) |
| 161 | +}); |
| 162 | + |
0 commit comments