mirror of https://github.com/dapr/quickstarts.git
87 lines
2.3 KiB
JavaScript
87 lines
2.3 KiB
JavaScript
// ------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
// ------------------------------------------------------------
|
|
|
|
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
require('isomorphic-fetch');
|
|
|
|
const app = express();
|
|
app.use(bodyParser.json());
|
|
|
|
const daprPort = process.env.DAPR_HTTP_PORT || 3500;
|
|
const stateStoreName = `statestore`;
|
|
const stateUrl = `http://localhost:${daprPort}/v1.0/state/${stateStoreName}`;
|
|
const port = 3000;
|
|
|
|
app.get('/order', (_req, res) => {
|
|
fetch(`${stateUrl}/order`)
|
|
.then((response) => {
|
|
if (!response.ok) {
|
|
throw "Could not get state.";
|
|
}
|
|
|
|
return response.text();
|
|
}).then((orders) => {
|
|
res.send(orders);
|
|
}).catch((error) => {
|
|
console.log(error);
|
|
res.status(500).send({message: error});
|
|
});
|
|
});
|
|
|
|
app.post('/neworder', (req, res) => {
|
|
const data = req.body.data;
|
|
const orderId = data.orderId;
|
|
console.log("Got a new order! Order ID: " + orderId);
|
|
|
|
const state = [{
|
|
key: "order",
|
|
value: data
|
|
}];
|
|
|
|
fetch(stateUrl, {
|
|
method: "POST",
|
|
body: JSON.stringify(state),
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
}
|
|
}).then((response) => {
|
|
if (!response.ok) {
|
|
throw "Failed to persist state.";
|
|
}
|
|
|
|
console.log("Successfully persisted state.");
|
|
res.status(200).send();
|
|
}).catch((error) => {
|
|
console.log(error);
|
|
res.status(500).send({message: error});
|
|
});
|
|
});
|
|
|
|
app.delete('/order/:id', (req, res) => {
|
|
const key = req.params.id;
|
|
console.log('Invoke Delete for ID ' + key);
|
|
|
|
const deleteUrl = stateUrl + '/' + key;
|
|
|
|
fetch(deleteUrl, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
}
|
|
}).then((response) => {
|
|
if (!response.ok) {
|
|
throw "Failed to delete state.";
|
|
}
|
|
|
|
console.log("Successfully deleted state.");
|
|
res.status(200).send();
|
|
}).catch((error) => {
|
|
console.log(error);
|
|
res.status(500).send({message: error});
|
|
});
|
|
});
|
|
|
|
app.listen(port, () => console.log(`Node App listening on port ${port}!`)); |