Skip to content

Commit

Permalink
Merge pull request #3 from aminduca/master
Browse files Browse the repository at this point in the history
+Added "start" script; +Added error handling
  • Loading branch information
aminduca authored Nov 22, 2018
2 parents eeabee7 + 32a5e2f commit bfcccc8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
38 changes: 24 additions & 14 deletions samples/node-token-server/Index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2005-2018 Coveo Solutions Inc.
import { CoveoSearchTokenGenerator, DynamicsPortalAuthTokenDecoder, IDecodedPortalAuthTokenPayload } from "coveo-search-token-generator";
import * as express from "express";
import * as http from "http";

// -----------------------------------------------------------------------------
// Change this configuration accordingly to represent your environment.
Expand Down Expand Up @@ -38,17 +39,26 @@ const getCoveoToken: express.RequestHandler = async (req: express.Request, res:
};

const port: string | number = process.env.PORT || 5950;
express().
use((req, res, next) => {
// Additional headers.
res.
header("Access-Control-Allow-Origin", config.portalUrl).
header("Access-Control-Allow-Methods", "POST,OPTIONS").
header("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length, X-Requested-With");
next();
}).
options("/*", (req: express.Request, res: express.Response) => res.send(200)).
get("/", (req, res) => res.status(200).send("Server is up and running.")).
post("/token", getCoveoToken).
use((req, res) => res.status(400).send({ error: "This request did not match any endpoint.", status: 400 })).
listen(port, () => console.info(`Token server listening on port ${port}.`));
const server: http.Server =
express()
.use((req, res, next) => {
// Additional headers.
res
.header("Access-Control-Allow-Origin", config.portalUrl)
.header("Access-Control-Allow-Methods", "POST,OPTIONS")
.header("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length, X-Requested-With");
next();
})
.options("/*", (req: express.Request, res: express.Response) => res.send(200))
.get("/", (req, res) => res.status(200).send("Token server is up and running."))
.post("/token", getCoveoToken)
.use((req, res) => res.status(400).send({ error: "This request did not match any endpoint.", status: 400 }))
.listen(port, () => console.info(`####### Token server listening on port ${port}. #######`)).
on("error", (error: any) => console.error(error.errno === "EADDRINUSE" ? `Server cannot start. There is already a process listening on port ${port}.` : error));

const stop = () => server.close(() => console.info(`Token server is stopped.`));
process
.on("exit", stop) // Application controlled exit event.
.on("SIGINT", stop) // Ctrl+C event.
.on("SIGUSR1", stop) // PID Killed events
.on("SIGUSR2", stop);
7 changes: 3 additions & 4 deletions samples/node-token-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ const config = {
};
```

## Build
## Running the Sample

To build this sample, open npm on the directory of this file. Then, run the following command :
To run this sample, open npm on the directory of this file and run the following command:

```bash
npm run setup
npm run build
npm run start
```

## Liability
Expand Down
1 change: 1 addition & 0 deletions samples/node-token-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"express": "^4.16.0"
},
"scripts": {
"start": "npm run setup && npm run build && node dist/Index.js",
"setup": "npm install",
"build": "npm run ts",
"ts": "npm run lint && node ./node_modules/typescript/bin/tsc --project tsconfig.json",
Expand Down

0 comments on commit bfcccc8

Please sign in to comment.