This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
71 lines (63 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var express = require('express');
var router = express.Router();
var liveConnect = require('../lib/liveconnect-client');
var createExamples = require('../lib/create-examples');
/* GET Index page */
router.get('/', function (req, res) {
var authUrl = liveConnect.getAuthUrl();
res.render('index', { title: 'OneNote API Node.js Sample', authUrl: authUrl});
});
/* POST Create example request */
router.post('/', function (req, res) {
var accessToken = req.cookies['access_token'];
var exampleType = req.body['submit'];
// Render the API response with the created links or with error output
var createResultCallback = function (error, httpResponse, body) {
if (error) {
return res.render('error', {
message: 'HTTP Error',
error: {details: JSON.stringify(error, null, 2)}
});
}
// Parse the body since it is a JSON response
var parsedBody;
try {
parsedBody = JSON.parse(body);
} catch (e) {
parsedBody = {};
}
// Get the submitted resource url from the JSON response
var resourceUrl = parsedBody['links'] ? parsedBody['links']['oneNoteWebUrl']['href'] : null;
if (resourceUrl) {
res.render('result', {
title: 'OneNote API Result',
body: body,
resourceUrl: resourceUrl
});
} else {
res.render('error', {
message: 'OneNote API Error',
error: {status: httpResponse.statusCode, details: body}
});
}
};
// Request the specified create example
switch (exampleType) {
case 'text':
createExamples.createPageWithSimpleText(accessToken, createResultCallback);
break;
case 'textimage':
createExamples.createPageWithTextAndImage(accessToken, createResultCallback);
break;
case 'html':
createExamples.createPageWithScreenshotFromHtml(accessToken, createResultCallback);
break;
case 'url':
createExamples.createPageWithScreenshotFromUrl(accessToken, createResultCallback);
break;
case 'file':
createExamples.createPageWithFile(accessToken, createResultCallback);
break;
}
});
module.exports = router;