imrefazekas / connect-rest

Exceptionally featureful Restful web services middleware for connect node.js
MIT License
100 stars 29 forks source link

Hello world example needed sorely #39

Closed luminarious closed 7 years ago

luminarious commented 7 years ago

Hey! I tried several hours today to get a hello world example running, but no luck (not even any errors), and yet the API and functionality is exactly what's needed for an internal service I have in mind for work.

const fs = require('fs')
const path = require('path')
const http = require('http')
const connect = require('connect')
const bodyParser = require('body-parser')
const Rest = require('connect-rest')

const PORT = process.env.PORT || 8000

// initial configuration of connect-rest. all-of-them are optional.
// default context is /api, all services are off by default
const rest = Rest.create({
    context: '/api',
    logger: { file: 'mochaTest.log', level: 'debug' },
    //apiKeys: [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ],
    discover: { path: 'discover', secure: false },
    // proto: { path: 'proto', secure: true }
})
const app = connect()
    .use(bodyParser.urlencoded({ extended: true }))
    .use(bodyParser.json())
    .use(rest.processRequest())

// defines a few sample rest services
const debug = (request, content, callback)=>{ 
    console.log( 'Received headers:' + JSON.stringify( request.headers ))
    console.log( 'Received parameters:' + JSON.stringify( request.parameters ) )
    console.log( 'Received JSON object:' + JSON.stringify( content ) )
    callback(null, 'ok')
}
rest.get('/books/:title/:chapter', debug)
rest.post( { path: '/make', version: '>=1.0.0' }, debug)
rest.post( [ '/act', '/do' ], debug)
rest.post( [ { path: '/shake', version: '>=2.0.0' }, { path: '/twist', version: '>=2.1.1' } ], debug)

http.createServer(app).listen(PORT, function () {
    console.log('Running on http://localhost:'+PORT)
})
imrefazekas commented 7 years ago

Could your describe your problem? I entered your code to my editor ran with node, opened browser with http://localhost:8000/api/books/a/b and I see the logs in the console and the 'ok' in the browser.

luminarious commented 7 years ago

You're right, the issue can be closed. I misunderstood the server message ("Cannot GET /") and though it was misconfigured somehow and forgot to check the books route.

imrefazekas commented 7 years ago

Sure, let me know if anything comes up...