[Issue 12622] Node.js guide - update example code (#15922)

* update server.js code
This commit is contained in:
Craig Osterhout 2022-10-20 13:32:12 -07:00 committed by GitHub
parent 8ebd41f2f6
commit ad614f1d2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 6 deletions

View File

@ -50,14 +50,30 @@ $ docker run -it --rm -d -v mongodb:/data/db \
Okay, now that we have a running MongoDB, lets update `server.js` to use MongoDB and not an in-memory data store.
```javascript
const ronin = require( 'ronin-server' )
const mocks = require( 'ronin-mocks' )
const ronin = require( 'ronin-server' )
const database = require( 'ronin-database' )
const server = ronin.server()
const mocks = require( 'ronin-mocks' )
database.connect( process.env.CONNECTIONSTRING )
server.use( '/', mocks.server( server.Router(), false, false ) )
server.start()
async function main() {
try {
await database.connect( process.env.CONNECTIONSTRING )
const server = ronin.server({
port: process.env.SERVER_PORT
})
server.use( '/', mocks.server( server.Router()) )
const result = await server.start()
console.info( result )
} catch( error ) {
console.error( error )
}
}
main()
```
Weve added the `ronin-database` module and we updated the code to connect to the database and set the in-memory flag to false. We now need to rebuild our image so it contains our changes.