Skip to content

Commit 44c8b99

Browse files
Add language type annotations and fix links in NodeServices readme
1 parent f0ec89b commit 44c8b99

File tree

1 file changed

+26
-26
lines changed
  • src/Microsoft.AspNetCore.NodeServices

1 file changed

+26
-26
lines changed

src/Microsoft.AspNetCore.NodeServices/README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ ASP.NET Core has a built-in dependency injection (DI) system. NodeServices is de
4747

4848
Enable NodeServices in your application by first adding the following to the top of your `Startup.cs` file:
4949

50-
```
50+
```csharp
5151
using Microsoft.AspNetCore.NodeServices;
5252
```
5353

5454
... and then add to your `ConfigureServices` method in that file:
5555

56-
```
56+
```csharp
5757
public void ConfigureServices(IServiceCollection services)
5858
{
5959
// ... all your existing configuration is here ...
@@ -65,7 +65,7 @@ public void ConfigureServices(IServiceCollection services)
6565

6666
Now you can receive an instance of `NodeServices` as a constructor parameter to any MVC controller, e.g.:
6767

68-
```
68+
```csharp
6969
using Microsoft.AspNetCore.NodeServices;
7070

7171
public class SomeController : Controller
@@ -83,7 +83,7 @@ public class SomeController : Controller
8383

8484
Then you can use this instance to make calls into Node.js code, e.g.:
8585

86-
```
86+
```csharp
8787
public async Task<IActionResult> MyAction()
8888
{
8989
var result = await _nodeServices.Invoke<int>("./addNumbers", 1, 2);
@@ -93,7 +93,7 @@ public async Task<IActionResult> MyAction()
9393

9494
Of course, you also need to supply the Node.js code you want to invoke. Create a file called `addNumber.js` at the root of your ASP.NET Core application, and add the following code:
9595

96-
```
96+
```javascript
9797
module.exports = function (callback, first, second) {
9898
var result = first + second;
9999
callback(/* error */ null, result);
@@ -110,7 +110,7 @@ If you want to put `addNumber.js` inside a subfolder rather than the root of you
110110

111111
In other types of .NET app where you don't have ASP.NET Core's DI system, you can get an instance of `NodeServices` as follows:
112112

113-
```
113+
```csharp
114114
// Remember to add 'using Microsoft.AspNetCore.NodeServices;' at the top of your file
115115
116116
var nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions());
@@ -127,7 +127,7 @@ You can dispose the `nodeServices` object whenever you are done with it (and it
127127

128128
**Signatures:**
129129

130-
```
130+
```csharp
131131
AddNodeServices()
132132
AddNodeServices(NodeServicesOptions options)
133133
```
@@ -136,21 +136,21 @@ This is an extension method on `IServiceCollection`. It registers NodeServices w
136136

137137
To access this extension method, you'll need to add the following namespace import to the top of your file:
138138

139-
```
139+
```csharp
140140
using Microsoft.AspNetCore.NodeServices;
141141
```
142142

143143
**Examples**
144144

145145
Using default options:
146146

147-
```
147+
```csharp
148148
services.AddNodeServices();
149149
```
150150

151151
Or, specifying options:
152152

153-
```
153+
```csharp
154154
services.AddNodeServices(new NodeServicesOptions
155155
{
156156
WatchFileExtensions = new[] { ".coffee", ".sass" },
@@ -163,7 +163,7 @@ services.AddNodeServices(new NodeServicesOptions
163163
* `options` - type: `NodeServicesOptions`
164164
* Optional. If specified, configures how the `NodeServices` instances will work.
165165
* Properties:
166-
* `HostingModel` - an `NodeHostingModel` enum value. See: [hosting models](#HostingModels)
166+
* `HostingModel` - an `NodeHostingModel` enum value. See: [hosting models](#hosting-models)
167167
* `ProjectPath` - if specified, controls the working directory used when launching Node instances. This affects, for example, the location that `require` statements resolve relative paths against. If not specified, your application root directory is used.
168168
* `WatchFileExtensions` - if specified, the launched Node instance will watch for changes to any files with these extensions, and auto-restarts when any are changed.
169169

@@ -175,15 +175,15 @@ If no `options` is passed, the default `WatchFileExtensions` array includes `.js
175175

176176
**Signature:**
177177

178-
```
178+
```csharp
179179
CreateNodeServices(NodeServicesOptions options)
180180
```
181181

182182
Directly supplies an instance of `NodeServices` without using ASP.NET's DI system.
183183

184184
**Example**
185185

186-
```
186+
```csharp
187187
var nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions {
188188
HostingModel = NodeHostingModel.Socket
189189
});
@@ -194,7 +194,7 @@ var nodeServices = Configuration.CreateNodeServices(new NodeServicesOptions {
194194
* `options` - type: `NodeServicesOptions`.
195195
* Configures the returned `NodeServices` instance.
196196
* Properties:
197-
* `HostingModel` - an `NodeHostingModel` enum value. See: [hosting models](#HostingModels)
197+
* `HostingModel` - an `NodeHostingModel` enum value. See: [hosting models](#hosting-models)
198198
* `ProjectPath` - if specified, controls the working directory used when launching Node instances. This affects, for example, the location that `require` statements resolve relative paths against. If not specified, your application root directory is used.
199199
* `WatchFileExtensions` - if specified, the launched Node instance will watch for changes to any files with these extension, and auto-restarts when any are changed.
200200

@@ -206,23 +206,23 @@ If you create a `NodeServices` instance this way, you can also dispose it (call
206206

207207
**Signature:**
208208

209-
```
209+
```csharp
210210
Invoke<T>(string moduleName, params object[] args)
211211
```
212212

213213
Asynchronously calls a JavaScript function and returns the result, or throws an exception if the result was an error.
214214

215215
**Example 1: Getting a JSON-serializable object from Node (the most common use case)**
216216

217-
```
217+
```csharp
218218
var result = await myNodeServicesInstance.Invoke<TranspilerResult>(
219219
"./Node/transpile",
220220
pathOfSomeFileToBeTranspiled);
221221
```
222222

223223
... where `TranspilerResult` might be defined as follows:
224224

225-
```
225+
```csharp
226226
public class TranspilerResult
227227
{
228228
public string Code { get; set; }
@@ -232,7 +232,7 @@ public class TranspilerResult
232232

233233
... and the corresponding JavaScript module (in `Node/transpile.js`) could be implemented as follows:
234234

235-
```
235+
```javascript
236236
module.exports = function (callback, filePath) {
237237
// Invoke some external transpiler (e.g., an NPM module) then:
238238
callback(null, {
@@ -244,7 +244,7 @@ module.exports = function (callback, filePath) {
244244

245245
**Example 2: Getting a stream of binary data from Node**
246246

247-
```
247+
```csharp
248248
var imageStream = await myNodeServicesInstance.Invoke<Stream>(
249249
"./Node/resizeImage",
250250
fullImagePath,
@@ -257,7 +257,7 @@ return File(imageStream, someContentType);
257257

258258
... where the corresponding JavaScript module (in `Node/resizeImage.js`) could be implemented as follows:
259259

260-
```
260+
```javascript
261261
var sharp = require('sharp'); // A popular image manipulation package on NPM
262262
263263
module.exports = function(result, physicalPath, maxWidth, maxHeight) {
@@ -286,15 +286,15 @@ There's a working image resizing example following this approach [here](https://
286286

287287
**Signature**
288288

289-
```
289+
```csharp
290290
InvokeExport<T>(string moduleName, string exportName, params object[] args)
291291
```
292292

293293
This is exactly the same as `Invoke<T>`, except that it also takes an `exportName` parameter. You can use this if you want your JavaScript module to export more than one function.
294294

295295
**Example**
296296

297-
```
297+
```csharp
298298
var someString = await myNodeServicesInstance.Invoke<string>(
299299
"./Node/myNodeApis",
300300
"getMeAString");
@@ -308,7 +308,7 @@ var someStringInFrench = await myNodeServicesInstance.Invoke<string>(
308308

309309
... where the corresponding JavaScript module (in `Node/myNodeApis.js`) could be implemented as follows:
310310

311-
```
311+
```javascript
312312
module.exports = {
313313

314314
getMeAString: function (callback) {
@@ -325,7 +325,7 @@ module.exports = {
325325
};
326326
```
327327

328-
**Parameters, return type, etc.** For all other details, see the docs for [`Invoke<T>`](#Invoke)
328+
**Parameters, return type, etc.** For all other details, see the docs for [`Invoke<T>`](#invoket)
329329

330330
## Hosting models
331331

@@ -347,7 +347,7 @@ People have asked about using [VroomJS](https://github.com/fogzot/vroomjs) as a
347347

348348
Normally, you can just use the default hosting model, and not worry about it. But if you have some special requirements, select a hosting model by passing an `options` parameter to `AddNodeServices` or `CreateNodeServices`, and populate its `HostingModel` property. Example:
349349

350-
```
350+
```csharp
351351
services.AddNodeServices(new NodeServicesOptions
352352
{
353353
HostingModel = NodeHostingModel.Socket
@@ -367,7 +367,7 @@ services.AddNodeServices(new NodeServicesOptions
367367

368368
If you implement a custom hosting model (by implementing `INodeServices`), then you can get instances of that just by using your type's constructor. Or if you want to designate it as the default hosting model that higher-level services (such as those in the `SpaServices` package) should use, register it with ASP.NET Core's DI system:
369369

370-
```
370+
```csharp
371371
services.AddSingleton(typeof(INodeServices), serviceProvider =>
372372
{
373373
return new YourCustomHostingModel();

0 commit comments

Comments
 (0)