Attribute | Details |
---|---|
Dapr runtime version | v0.9 |
Language | Javascript |
Environment | Local |
This tutorial and sample will demonstrate how to get Dapr running locally on your machine without docker. You will be deploying a Node.js app that subscribes to order messages demonstrating service invocation capability of Dapr. The following architecture diagram illustrates the components that make up this sample:
This sample requires you to have the following installed on your machine:
- Node.js version 8 or greater
- Postman [Optional]
Follow instructions to download and install the Dapr CLI and initialize Dapr.
Now that Dapr has been setup locally, clone the repo, then navigate to the hello-dapr-slim
sample:
git clone https://github.com/dapr/samples.git
cd samples/hello-dapr-slim
In the app.js
you'll find a simple express
application, which exposes a single route and handler.
Take a look at the neworder
handler:
app.post('/neworder', bodyParser.json(), (req, res) => {
const data = req.body.data;
const orderId = data.orderId;
console.log("Got a new order! Order ID: " + orderId);
res.status(200).send("Got a new order! Order ID: " + orderId);
});
Here the endpoint neworder
, will receive and handle messages. The handler logs the incoming message, and respond with the same log.
-
Install dependencies:
npm install
This will install
express
andbody-parser
, dependencies that are shown inpackage.json
. -
Run Node.js app with Dapr:
dapr run --app-id nodeapp --app-port 3000 --dapr-http-port 3500 node app.js
The command should output text that looks like the following, along with logs:
Starting Dapr with id nodeapp. HTTP Port: 3500. gRPC Port: 9165
You're up and running! Both Dapr and your app logs will appear here.
...
Note: the
--app-port
(the port the app runs on) is configurable. The Node app happens to run on port 3000, but you could configure it to run on any other port. Also note that the Dapr--app-port
parameter is optional, and if not supplied, a random available port is used.
Now that Dapr and the Node.js app are running, you can POST messages against it, using different tools. Note: here you are POSTing against port 3500 - if you used a different port, be sure to update your URL accordingly.
First, POST the message by using Dapr cli in a new command line terminal:
Windows Command Prompt
dapr invoke --verb POST --app-id nodeapp --method neworder --data "{\"data\": { \"orderId\": \"41\" } }"
Windows PowerShell
dapr invoke --verb POST --app-id nodeapp --method neworder --data '{\"data\": { \"orderId\": \"41\" } }'
Linux or MacOS
dapr invoke --verb POST --app-id nodeapp --method neworder --data '{"data": { "orderId": "41" } }'
Now, you can also do this using curl
with:
curl -XPOST -d @sample.json -H "Content-Type:application/json" http://localhost:3500/v1.0/invoke/nodeapp/method/neworder
Or, you can also do this using the Visual Studio Code Rest Client Plugin
POST http://localhost:3500/v1.0/invoke/nodeapp/method/neworder
{
"data": {
"orderId": "42"
}
}
You can also use the Postman GUI.
Open Postman and create a POST request against http://localhost:3500/v1.0/invoke/nodeapp/method/neworder
In your terminal window, you should see logs indicating that the message was received and state was updated:
== APP == Got a new order! Order ID: 42
== APP == Successfully persisted state.
Now, to make sure that the method definition was successfully invoked, verify that the the response is:
Got a new order! Order ID: 41
Additionally the log in the node application started with Dapr should be
== APP == Got a new order! Order ID: 41
To stop your service from running, simply stop the "dapr run" process. Alternatively, you can spin down your service with the Dapr CLI "stop" command. For example, to spin down the service, run this command in a new command line terminal:
dapr stop --app-id nodeapp
To see that services have stopped running, run dapr list
, noting that your services no longer appears!
With the run of dapr init --slim
, only the binaries daprd
and placement
are installed in the system. In the normal default init, dapr init
placement service is installed as a container and additionally redis and zipkin containers are run for enabling state store and tracing, which requires Docker to be installed as a prerequisite. See environment setup for more information. The slim init provides only limited functionaity unless you configure a state store yourself.
Now that you've gotten Dapr running locally on your machine, consider these next steps:
- Learn more about Dapr in the Dapr overview documentation.
- Explore Dapr concepts such as building blocks and components in the Dapr concepts documentation.