Json-server: Support for HTTPS

Created on 6 Mar 2016  路  15Comments  路  Source: typicode/json-server

It would be nice, if json-server could support HTTPS. Then prototyping via json-server would even more realistic.

Most helpful comment

This works:

var fs = require('fs'),
  https = require('https'),
  jsonServer = require('json-server'),
  server = jsonServer.create(),
  router = jsonServer.router('db.json'),
  middlewares = jsonServer.defaults();

var options = {
  key: fs.readFileSync('./ssl/key.pem'),
  cert: fs.readFileSync('./ssl/cert.pem')
};

server.use(middlewares);
server.use(router);

https.createServer(options, server).listen(3002, function() {
  console.log("json-server started on port " + 3002);
});

You can use openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 120 -nodes to generate self-signed certificate and private key.

All 15 comments

+1

-1 Can't you put your json-server behind an Apache or Nginx with a cert installed and proxy to it? That's also a realistic scenario. I think adding traffic encryption/decryption and SSL cert handling would be nice but would also be well past the scope of the original project.

Thanks @managedkaos, I created an IIS reverse proxy rule to do this. I don't think the project needs to support HTTPS anymore.

Awesome! :D

As a simpler alternative to setting up an Apache or Nginx server, you can give a try to hotel:

my-project $ hotel add 'json-server db.json'
# https://my-project.dev

@Karankang007 How can I do this? any article?

This works:

var fs = require('fs'),
  https = require('https'),
  jsonServer = require('json-server'),
  server = jsonServer.create(),
  router = jsonServer.router('db.json'),
  middlewares = jsonServer.defaults();

var options = {
  key: fs.readFileSync('./ssl/key.pem'),
  cert: fs.readFileSync('./ssl/cert.pem')
};

server.use(middlewares);
server.use(router);

https.createServer(options, server).listen(3002, function() {
  console.log("json-server started on port " + 3002);
});

You can use openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 120 -nodes to generate self-signed certificate and private key.

@nishantshreshth thank you indeed!

@Karankang007 @managedkaos @juergenzimmermann @nilocoelhojunior @Gribbs
Better option is to use hotel with json-server. I recently set it up. Please see this URL for easy to follow steps.
https://gist.github.com/abhatia1176/3ee1173df2b07ed5e90761bbdfe526d0
Thanks @typicode for this beautiful tool.

Thanks @nishantshreshth for sharing this solution :)
Thanks @abhatia1176 for the nice comment, glad you like it :)

@abhatia1176 somehow your link is _mangled_ in https://github.com/typicode/json-server/issues/244#issuecomment-396508227, you have to copy it and paste it instead of clicking on it.

@nishantshreshth Hello,
I am new to json-server, anyone could please kindly tell how to use below? Great thanks!

I did as below:
~
[nodejs@xiu-v3-test ~]$ ls
db.json node_modules node-v10.13.0-linux-x64.tar.xz ssl
https.js node-v10.13.0-linux-x64 package-lock.json
~

https.js is just the content from below. And when I run as below, error occurred:
~~~
[nodejs@xiu-v3-test ~]$ node https.js
/home/nodejs/node_modules/json-server/lib/server/router/index.js:44
validateData(db.getState()); // Add lodash-id methods to db
^

TypeError: db.getState is not a function
at Object.module.exports [as router] (/home/nodejs/node_modules/json-server/lib/server/router/index.js:44:19)
at Object. (/home/nodejs/https.js:5:23)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
~~~

This works:

var fs = require('fs'),
  https = require('https'),
  jsonServer = require('json-server'),
  server = jsonServer.create(),
  router = jsonServer.router('db.json'),
  middlewares = jsonServer.defaults();

var options = {
  key: fs.readFileSync('./ssl/key.pem'),
  cert: fs.readFileSync('./ssl/cert.pem')
};

server.use(middlewares);
server.use(router);

https.createServer(options, server).listen(3002, function() {
  console.log("json-server started on port " + 3002);
});

You can use openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 120 -nodes to generate self-signed certificate and private key.

Upgraded to 0.14.2 and problem resovled ...

@nishantshreshth thank you very much

This works:

var fs = require('fs'),
  https = require('https'),
  jsonServer = require('json-server'),
  server = jsonServer.create(),
  router = jsonServer.router('db.json'),
  middlewares = jsonServer.defaults();

var options = {
  key: fs.readFileSync('./ssl/key.pem'),
  cert: fs.readFileSync('./ssl/cert.pem')
};

server.use(middlewares);
server.use(router);

https.createServer(options, server).listen(3002, function() {
  console.log("json-server started on port " + 3002);
});

You can use openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 120 -nodes to generate self-signed certificate and private key.

Kudos to you. This is a great little solution.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0plus1 picture 0plus1  路  3Comments

goldmont picture goldmont  路  3Comments

TXRRNT picture TXRRNT  路  4Comments

sboudouk picture sboudouk  路  3Comments

AdamCook44 picture AdamCook44  路  3Comments