Code Monkey home page Code Monkey logo

Comments (8)

typicode avatar typicode commented on May 23, 2024

Yes.

Something like that should work:

var express = require('express');
var jsonServer = require('json-server')

var db = {
  posts: [
    { id: 1, body: 'foo' }
  ]
}

var server = express()

// custom routes
server.post('/login', function(req, res) {
    res.json({name: "John Doe", access_token:"3A364F68DA5745EB653457A585678"});
})

// mount jsonServer
server.use(jsonServer(db))

server.listen(3000)

It comes from this thread #30

Does it solve your problem?

from json-server.

gpolyn avatar gpolyn commented on May 23, 2024

Yes, this solves my problem.

However, I have further motivation that is unaddressed by custom routing, on which I invite your comment. json-server is so handy for use as a local test/dev server, but I can't figure out how to "refresh" the server from the initial dataset, as needed in testing. Any comment on doing this with json-server (or anything else) much appreciated.

G

from json-server.

typicode avatar typicode commented on May 23, 2024

Cool.

Not sure to understand what you mean by "refresh"? Could you give me an example?

from json-server.

gpolyn avatar gpolyn commented on May 23, 2024

Yes, thanks.

My problem is likely mundane, but obscure, I guess, and I have not yet
found a solution from other sources.

I am using json-server to represent a "real" REST service for integration
testing of javascript clients, e.g., an Angular app. I need to refresh the
test data in my json-server after certain automated tests. So, I would add
a custom route to my json-server, like as follows:
...
var expressServer = express();
expressServer.post('/refresh-data', function(req, res) {
// refresh the json-server: access the lowDB instance and read back in
the initial data file
});
expressServer.use(server(obj)).listen(3000);
...

On Mon, Nov 10, 2014 at 2:51 AM, typicode [email protected] wrote:

Cool.

Not sure to understand what you mean by "refresh"? Could you give me an
example?


Reply to this email directly or view it on GitHub
#33 (comment).

from json-server.

typicode avatar typicode commented on May 23, 2024

Ok I see.
It's something missing in the project API and I should add it.

I'll try to do it this week.

from json-server.

typicode avatar typicode commented on May 23, 2024

Actually, using lodash, you could do this:

var _ = require('lodash')

var db = {
  // ...
}

var original = _.cloneDeep(db)

function reset() {

  // Empty db object
  _.each(db, function(value, key) {
    delete db[key]
  })

  // Copy original into db
  _.extend(db, _.cloneDeep(original))

}

jsonServer(db)

from json-server.

gpolyn avatar gpolyn commented on May 23, 2024

Your reset() is shown to work in my 1-2 below, but the server does not
reflect the change for any subsequent GET request...

expressServer.post('/refresh-data', function(req, res) {
console.log("data before reset: " + JSON.stringify(obj)) // 1
reset();
console.log("data after reset: " + JSON.stringify(obj)) // 2
res.end();
});

expressServer.use(server(obj)).listen(3000);

G

On Mon, Nov 10, 2014 at 1:56 PM, typicode [email protected] wrote:

Actually, using lodash, you could do this:

var _ = require('lodash')
var db = {
// ...
}
var original = _.cloneDeep(db)
function reset() {

// Empty db object
_.each(db, function(value, key) {
delete db[key]
})

// Copy original into db
_.extend(db, _.cloneDeep(original))

}

jsonServer(db)


Reply to this email directly or view it on GitHub
#33 (comment).

from json-server.

bhushangahire avatar bhushangahire commented on May 23, 2024

I am trying to use this for angular app is there any example that I can use. I am new to this.

from json-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.