How to use the createServer function from http
Find comprehensive JavaScript http.createServer code examples handpicked from public code repositorys.
http.createServer is a method in the Node.js HTTP module that creates an HTTP server object that can listen for incoming HTTP requests and send back responses.
GitHub: cliss/camel
47 48 49 50 51 52 53 54 55 56 57
app.use(express.static("public")); app.use(function (request, response, next) { response.header('X-powered-by', 'Camel (https://github.com/cliss/camel)'); next(); }); var server = http.createServer(app); // "Statics" var postsRoot = './posts/'; var templateRoot = './templates/';
+ 3 other calls in file
8 9 10 11 12 13 14 15 16 17 18 19 20
const clock = FakeTimers.createClock() t.autoend(false) const target = http.createServer((req, res) => { t.pass('request proxied') req.on('data', () => undefined) req.on('end', () => { res.flushHeaders()
How does http.createServer work?
http.createServer is a method in the Node.js HTTP module that creates an HTTP server object that can listen for incoming HTTP requests and send back responses. When you call http.createServer([options], [requestListener]), the function returns a new http.Server object. You can specify optional options to configure the server, such as the server's timeout or maxHeadersCount. The requestListener is a callback function that will be called every time the server receives an HTTP request. The function takes two arguments: request, which is an http.IncomingMessage object representing the incoming request, and response, which is an http.ServerResponse object representing the server's response to the request. Inside the requestListener function, you can read the incoming request headers and data, and send back a response to the client using the response object, which has methods such as writeHead and end. Once you have created an HTTP server with http.createServer, you can start listening for incoming requests using the server.listen(port, [hostname], [backlog], [callback]) method, which will bind the server to the specified port and hostname. In essence, http.createServer provides a way to create an HTTP server that can handle incoming HTTP requests and send back responses, making it easy to build HTTP-based applications and services in Node.js.
118 119 120 121 122 123 124 125 126
if you have an http server that you also need to serve stuff over, and want to use a single port, use the `server` option. ``` js var http = require('http') var server = http.createServer(function(req, res){...}).listen(....) ws.createServer({server: server}, function (stream) { ... }) ```
GitHub: Qbix/Platform
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
console.log('Error while updating secure context: ' + error.message); } }, 5000); }); } else { server = http.createServer(_express); } } else { server = express.createServer(); _express = server;
Ai Example
1 2 3 4 5 6 7 8 9 10
const http = require("http"); const server = http.createServer((request, response) => { response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Hello, world!\n"); }); server.listen(3000, () => { console.log("Server listening on port 3000"); });
In this example, we first import the http module. We then create an HTTP server using http.createServer. The server's requestListener function takes a request object representing the incoming request, and a response object representing the server's response to the request. In this case, we simply send back a plain text response of "Hello, world!". We then call server.listen(3000) to bind the server to port 3000. When you run this script and access http://localhost:3000/ in your web browser, you should see the message "Hello, world!" displayed on the page.
203 204 205 206 207 208 209 210 211 212
+const common = require('../common'); +const assert = require('assert'); +const http = require('http'); +const net = require('net'); + +const server = http.createServer(common.mustCall((req, res) => { + res.end(); +}, 2)); + +server.keepAliveTimeout = common.platformTimeout(100);
+ 5 other calls in file
GitHub: zhyupe/port-mapper
18 19 20 21 22 23 24 25 26 27
en: '<div class="notice"><p>Succeed! </p></div>', zh_CHS: '<div class="notice"><p>授权成功!</p></div>' } }; var server = http.createServer(function(req, res) { if (req.method != 'GET') { res.writeHead(405, 'Method Not Allowed'); res.write('Sorry, but the auth server only allows GET requests.'); res.end();
77 78 79 80 81 82 83 84 85 86
``` js var http = require('http'); var fs = require('fs'); var server = http.createServer(function (req, res) { fs.readFile(__dirname + '/data.txt', function (err, data) { if (err) { res.statusCode = 500; res.end(String(err));
+ 7 other calls in file
111 112 113 114 115 116 117 118 119 120
that implements an HTTP server: ```js const http = require('http'); const server = http.createServer((req, res) => { // `req` is an http.IncomingMessage, which is a Readable Stream. // `res` is an http.ServerResponse, which is a Writable Stream. let body = '';
160 161 162 163 164 165 166 167 168 169 170 171 172
init(); } const requestListenerWrapper = module.exports.generateRequestListener(options); return http.createServer(requestListenerWrapper); }; /** * Provides the GET /health endpoint
GitHub: dynatrace-oss/unguard
161 162 163 164 165 166 167 168 169 170 171
}); // register all the routes app.use(process.env.FRONTEND_BASE_PATH, site); const server = http.createServer(app) server.listen('3000', () => { frontendLogger.info('Listening on port 3000'); });
GitHub: lucalysoft/slides
3 4 5 6 7 8 9 10 11 12 13 14 15
var io = require('socket.io'); var crypto = require('crypto'); var app = express(); var staticDir = express.static; var server = http.createServer(app); io = io(server); var opts = {
GitHub: JayAgra/scouting-app
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
app.get('/offline.html', function(req, res) { res.sendFile('src/offline.html', { root: __dirname }) }); // deepcode ignore HttpToHttps: ignoring because it is used only to redirect requests to HTTPS, deepcode ignore OR: ignored because it is redirecting to HTTPS, deepcode ignore OR: <please specify a reason of ignoring this> if (certsizes.key <= 100 || certsizes.cert <= 100) {app.listen(80)} else {const httpRedirect = express(); httpRedirect.all('*', (req, res) => res.redirect(`https://${req.hostname}${req.url}`)); const httpServer = http.createServer(httpRedirect); httpServer.listen(80, () => logInfo(`HTTP server listening: http://localhost`));} //server created and ready for a request logInfo("Ready!");
20 21 22 23 24 25 26 27 28 29 30 31 32
} = process.env console.log(`行先掲示板 v${version}`) const app = express() const http_server = http.createServer(app) const io = socketio(http_server) exports.io = io const { update_whereabouts } = require("./controllers/users.js")
4 5 6 7 8 9 10 11 12 13 14 15 16
const path = require("path"); const express = require("express"); const app = express(); const server = http.createServer(app); const io = socketio(server); const { addUser,
GitHub: mengqiuleo/y-terminal
67 68 69 70 71 72 73 74 75 76
// maxAge: 1000 * 60 * 60 * 24 * 30, // 30 天后过期 // httpOnly: true, // 是否允许客户端修改 cookie(默认 true 不能被修改) // }, // }; // this.app.use(expressSession(sessionOptions)); this.server = http.createServer(this.app) } setRoute(path, handlerFunction) { const handler = async (req, res) => {
+ 10 other calls in file
84 85 86 87 88 89 90 91 92
}) } // create an express server const expressApp = express() const server = http.createServer(expressApp) // watch files with socketIO const io = SocketIO(server)
GitHub: sumitdhyani/binanceWeb
2 3 4 5 6 7 8 9 10 11
const httpHandle = require('http') const CommonUtils = require('../CommonUtils') const api = require('../apiHandle') const Socket_io = require('socket.io') const { SubscriptionHandler } = require('./SubscriptionHandler') const httpServer = httpHandle.createServer(app) const io = new Socket_io.Server(httpServer) const appSpecificErrors = require('../appSpecificErrors') const SpuriousUnsubscription = appSpecificErrors.SpuriousUnsubscription const DuplicateSubscription = appSpecificErrors.DuplicateSubscription
GitHub: givikuna/ketoPhotography
81 82 83 84 85 86 87 88 89 90 91
return imgLoc; } } if (!module.parent) { http.createServer(function (req, res) { try { var infoFromURL = url.parse(req.url, true).query; // takes the information from the URL res.writeHead(200, { "Access-Control-Allow-Origin": "*" });
448 449 450 451 452 453 454 455 456 457 458
function startServer() { console.log( 'Starting HTTP server on', `http://${cfg.httpServerListenHost}:${cfg.httpServerListenPort}`); http.createServer(function(req, res) { console.debug(req.method, req.url); let uri = req.url.split('?', 1)[0]; if (uri.endsWith('/')) { uri += 'index.html';
47 48 49 50 51 52 53 54 55 56
way. Here is an example of using Streams in an Node.js program: ```js const http = require('http'); var server = http.createServer( (req, res) => { // req is an http.IncomingMessage, which is a Readable Stream // res is an http.ServerResponse, which is a Writable Stream var body = '';
+ 3 other calls in file
http.createServer is the most popular function in http (322 examples)