- download source
- unzip the source code
- go into directory and type npm install to install the modules
- Once the packages are installed, run it npm run dev
- Go to localhost:8080, then check terminal for logs
Set up
Open terminal, navigate to ‘Desktop’
mkdir dnyNodeAPI
cd dnyNodeAPI
npm init
press enter for all selections
npm install express
Then check your node_modules folder and you’ll see whole of packages being installed.
In our package.json file, change ‘main’: ‘index.js’ to ‘main’: ‘app.js’
Create file ‘app.js’:
1 2 3 4 5 6 7 8 9 10 11 12 |
const express = require('express') const app = express() app.get("/", (req, res) => { console.log(this) res.send("Hello world from DNY tourism app") }) const port = 8080 app.listen(port, () => { console.log(`DNY tourism app listening on port ${port}`) }) |
in your terminal, if you went node app.js, it will run the server on port 8080.
But whenever we make a code change, we have to restart the server and this becomes cumbersome. So let’s install nodemon.
npm i nodemon
Then in your package.json:
1 2 3 |
"scripts": { "dev": "nodemon app.js" }, |
Now, in your terminal, Ctrl+C to get out of whatever is running.
Then npm run dev.
It will run the command as specified in our package.json and we get the server running with constant updates.
Putting in Routes
routes/posts.js
1 2 3 |
exports.getPosts = (req, res) => { res.send("Hello world from DNY tourism app") } |
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const express = require('express') const app = express() // bring in routes const { getPosts } = require('./routes/post') app.get("/", getPosts) const port = 8080; app.listen(port, () => { console.log(`DNY tourism app listening on port ${port}`) }) |
Install Middleware
npm i morgan
Installing Database
Create account in mLab, and create a database. Select default for everything, i.e AWS, server at Europe, and create submit. Then ‘create db user’, and create a username and pwd for the db’s user.
Then, in your project, create a file .env:
.env
1 2 |
MONGO_URI=mongodb://<username>:<password>@ds263368.mlab.com:63368/dny-tours-api PORT=8080 |
npm i dotenv
npm i mongoose
app.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 |
const express = require('express') const app = express() const mongoose = require('mongoose') const morgan = require("morgan"); const dotenv = require('dotenv') dotenv.config() // db mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log('DB Connected')) mongoose.connection.on('error', err => { console.log(`DB connection error ${err.message}`) }); // bring in routes const postRoutes = require('./routes/post') app.use(morgan('dev')) app.use("/", postRoutes) const port = process.env.PORT || 8080; app.listen(port, () => { console.log(`DNY tourism app listening on port ${port}`) }) |
post.js
1 2 3 4 5 6 7 8 9 |
const express = require('express') const postController = require('../controllers/post') // have access to router const router = express.Router() router.get('/', postController.getPosts) module.exports = router; |
post.js
1 2 3 4 5 6 |
exports.getPosts = (req, res) => { //res.send("controllers/post.js - hello wordl from node js"); res.json({ posts: [{title: 'First post'}, {title: 'Second post'}] }) } |