Skip to content

Commit 12806f0

Browse files
committed
Now we can use an external file with our API routes using ExpressJS syntax
1 parent ef67b44 commit 12806f0

6 files changed

Lines changed: 43 additions & 6 deletions

File tree

Cakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ commands =
264264
watch: 'compile our files initially, and again for each change (runs install)'
265265
test: 'run our tests (runs compile)'
266266
prepublish: 'prepare our package for publishing'
267-
publish: 'publish our package (runs prepublish)'
267+
publish: 'publish our package'
268268

269269
Object.keys(commands).forEach (key) ->
270270
description = commands[key]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "docpad-plugin-api",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "DocPad plugin that inyects your custom REST api inside a site built with Docpad.",
55
"homepage": "https://github.com/univunix/docpad-plugin-api",
66
"license": "MIT",

src/api.plugin.coffee

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
path = require('path')
12
# Export Plugin
23
module.exports = (BasePlugin) ->
34
# Define Plugin
@@ -6,17 +7,22 @@ module.exports = (BasePlugin) ->
67
name: 'api'
78

89
config:
9-
apiSource: ""
10+
apiSource: ''
1011

11-
# The rest of your plugin definition goes here
12-
# ...
1312
serverExtend: (opts) ->
1413
#Extract server from options.
1514
{server} = opts
15+
docpad = @docpad
16+
rootPath = docpad.getConfig().rootPath
17+
customApi = require(path.join(rootPath, @config.apiSource))
1618

19+
# Default route.
1720
server.get '/api/engine/version', (req, res, next) ->
1821
res.json({
1922
name: 'docpad-plugin-api',
2023
dev: 'UnivUnix',
2124
version: '2.0.0'
2225
})
26+
27+
# Go to custom API routes.
28+
customApi(opts)

src/api.tester.coffee

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ module.exports = (testers) ->
2424
outExpectedPath = tester.config.outExpectedPath
2525
plugin = tester.docpad.getPlugin('api')
2626

27-
test 'api/engine/version url', (done) ->
27+
test 'GET api/engine/version url', (done) ->
2828
fileUrl = "#{baseUrl}/api/engine/version"
2929
request fileUrl, (err,response,actual) ->
3030
return done(err) if err
3131
actualStr = actual.match(/docpad-plugin-api/)
3232
expectedStr ='docpad-plugin-api'
3333
expect(actualStr[0]).to.equal(expectedStr)
3434
done()
35+
36+
test 'GET api/test', (done) ->
37+
fileUrl = "#{baseUrl}/api/test"
38+
request fileUrl, (err, response, actual) ->
39+
return done(err) if err
40+
actualStr = actual.match(/OK/)
41+
expectedStr = 'OK'
42+
expect(actualStr[0]).to.equal(expectedStr)
43+
done()

test/api/api-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = function (opts) {
2+
var server = opts.server
3+
4+
server.get('/api/test', function (req, res, next) {
5+
return res.json({
6+
test: 'OK'
7+
})
8+
})
9+
}

test/docpad.coffee

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# DocPad Configuration File
2+
# http://docpad.org/docs/config
3+
4+
# Define the DocPad Configuration
5+
docpadConfig = {
6+
# Plugins configuration
7+
plugins:
8+
api:
9+
apiSource: 'api/api-test.js'
10+
}
11+
12+
# Export the DocPad Configuration
13+
module.exports = docpadConfig

0 commit comments

Comments
 (0)