<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shaun Collins</title>
    <description>The latest articles on Forem by Shaun Collins (@ajax27).</description>
    <link>https://forem.com/ajax27</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F77713%2F0ae6b5ee-9e38-4c95-a33e-2f1d3b0f0454.jpeg</url>
      <title>Forem: Shaun Collins</title>
      <link>https://forem.com/ajax27</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ajax27"/>
    <language>en</language>
    <item>
      <title>SEO Freindly Blog</title>
      <dc:creator>Shaun Collins</dc:creator>
      <pubDate>Tue, 19 Nov 2019 17:37:15 +0000</pubDate>
      <link>https://forem.com/ajax27/seo-freindly-blog-2f11</link>
      <guid>https://forem.com/ajax27/seo-freindly-blog-2f11</guid>
      <description>&lt;p&gt;SEO Friendly Blog&lt;/p&gt;

&lt;p&gt;I have been learning to code for about 3 years now and have built countless Blogs so I thought it was about time I shared what I have learnt!  I will lay out this build in small chunks as I go through the process. All feedback is welcome including criticism but please be kind!&lt;/p&gt;

&lt;p&gt;Step One&lt;br&gt;
First lets make the folder structure with mkdir 'project-name' then create 2 further directories 'backend' and 'frontent'. Change directory to backend and run npm init -y to create a package.json file and install express, body-parser, cookie-parser, nodemon, cors, morgan and dotenv. When the install has completed don't forget to create a .gitignore file and .env file, enter your node_modules and .env in the gitignore file as you don't want to upload these to Github!&lt;br&gt;
Now, still in your backend directory create server.js file and populate it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
require("dotenv").config();


const app = express();


// Middlewares
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());


// Routes
app.get("/api", (req, res) =&amp;gt; res.json({ time: Date().toString() }));


// Port
const port = process.env.PORT || 8000;
app.listen(port, () =&amp;gt; console.log(`App running on port: ${port}`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then lets change the json file so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "backend",
  "version": "1.0.0",
  "description": "SEO Blog Backend",
  "main": "index.js",
  "scripts": {
    "start": "nodemon server.js"
  },
  "keywords": [
    "node",
    "react",
    "seo"
  ],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.7.11",
    "morgan": "^1.9.1",
    "nodemon": "^1.19.4"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you set your enviroment variables in your env file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NODE_ENV=development
PORT=8000
CLIENT_URL=http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are ready to start our server with npm run start. Visit localhost:8000/api and you should see your local time displayed in json format, if you don't get a json formatter extension to parse the output.&lt;/p&gt;

&lt;p&gt;Step Two&lt;br&gt;
Next we need to run some requests to the API, for that visit &lt;a href="https://www.getpostman.com/"&gt;https://www.getpostman.com/&lt;/a&gt; and download Postman. As we will be sending requests from localhost: 3000 with our frontend React app we need to configure the CORS Middleware as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// CORS Cofig
if (process.env.NODE_ENV == 'development') {
  app.use(cors({ origin: `${process.env.CLIENT_URL}`}));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Otherwise we get  Access-Control-Allow-Origin error in your browser, Postman will not be affected by this.&lt;/p&gt;

&lt;p&gt;Step Three&lt;br&gt;
Next lets connect our Database, for this you have to either open a MongoDB Atlas account or download MongoDB to your machine and run it locally whatever is your preference, personally I like to use Atlas it is very easy to setup. Create a cluster and name it what you like. To connect your app just choose connect to application and mongodb supplies a link, copy to your clipboard and return to your env file to paste in your link like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NODE_ENV=development
PORT=8000
CLIENT_URL=http://localhost:3000
DATABASE='mongodb+srv://USERNAME:PASSWORD@seoblog-dhag5.mongodb.net/DBNAME?retryWrites=true&amp;amp;w=majority'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in server.js file require in mongoose and configure your DB connection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();


const app = express();


// Connect Database
mongoose
  .connect(process.env.DATABASE, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    useFindAndModify: false
  })
  .then(() =&amp;gt; console.log("Database is Connected!"))
  .catch(err =&amp;gt; console.log(err));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart your server and you should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
App running on port: 8000
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
App running on port: 8000
Database is Connected!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As I said, you can install MongoDB locally if that is your preference, here are some links:&lt;/p&gt;

&lt;p&gt;Installing MongoDB on MAC&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/"&gt;https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installing MongoDB on Windows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/"&gt;https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installing Robo3T&lt;/p&gt;

&lt;p&gt;&lt;a href="https://robomongo.org/"&gt;https://robomongo.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you use Atlas you can download MongoDB Compass and install from your Cluster, just choose connect with Compass!&lt;/p&gt;

&lt;p&gt;If you use local mongodb then connect with this string in your env file:&lt;/p&gt;

&lt;p&gt;DATABASE_LOCAL='mongodb://localhost:27017/seoblog'&lt;/p&gt;

&lt;p&gt;Don't forget to adjust your server file to accommodate this change!&lt;/p&gt;

&lt;p&gt;Step Four ROUTES&lt;/p&gt;

&lt;p&gt;Create a new routes folder in the root of your backend directory and the create a blog.js file in that folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const router = express.Router();


router.get("/", (req, res) =&amp;gt; {
  res.json({ time: Date().toString() });
});


module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// and in your server.js file require your blog.js file and setup Routes Middleware&lt;/p&gt;

&lt;p&gt;// Routes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const blogRoutes = require('./routes/blog');


const app = express();


// Connect Database
mongoose
  .connect(process.env.DATABASE_LOCAL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    useFindAndModify: false
  })
  .then(() =&amp;gt; console.log("Database is Connected!"))
  .catch(err =&amp;gt; console.log(err));


// Middlewares
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(cookieParser());


// Routes Middleware
app.use(blogRoutes);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can remove the API route in server.js and use the api as the first argument in your new middleware:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use('/api', blogRoutes);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step Five Controllers&lt;/p&gt;

&lt;p&gt;Next as with the routes we need to make a controllers folder in the root directory with a blog.js file.&lt;/p&gt;

&lt;p&gt;// controllers blog.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.time = (req, res) =&amp;gt; {
  res.json({ time: Date().toString() });
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// Then in your routes blog.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const router = express.Router();
const { time } = require("../controllers/blog");


router.get("/", time);


module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tomorrow I'll come back and show how I add User SignUp and SignIn Functionality.&lt;/p&gt;

&lt;p&gt;Happy Coding&lt;/p&gt;

</description>
      <category>node</category>
      <category>react</category>
      <category>nextjs</category>
      <category>seo</category>
    </item>
  </channel>
</rss>
