Skip to content

Commit

Permalink
Support Node http.createServer()
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmoscrop committed Oct 20, 2022
1 parent fd57f46 commit 8e083c6
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Thank you to Upstash for reaching out to sponsor this project!
### Supported Frameworks
(* Experimental)

* Node (http.createServer)
* Connect
* Express
* Koa
Expand Down
9 changes: 9 additions & 0 deletions lib/framework/get-framework.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const http = require('http')
const Response = require('../response');

function common(cb) {
Expand All @@ -13,6 +14,14 @@ function common(cb) {
}

module.exports = function getFramework(app) {
if (app instanceof http.Server) {
return request => {
const response = new Response(request);
app.emit('request', request, response)
return response
}
}

if (typeof app.callback === 'function') {
return common(app.callback());
}
Expand Down
110 changes: 110 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const http = require('http'),
expect = require('chai').expect,
request = require('./util/request');

describe('node http server', () => {
let app;

it('should set statusCode and default body', () => {
app = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.write('Hello, world!')
res.end()
})
return request(app, {
httpMethod: 'GET',
path: '/'
})
.then(response => {
expect(response.statusCode).to.equal(200);
expect(response.body).to.equal('Hello, world!');
});
})
})

0 comments on commit 8e083c6

Please sign in to comment.