Use Node.

Tutorial — Building a Simple Blog With Node, Express & Mongo

The Essentials

cd ~/projects
mkdir node-blog
cd node-blog
touch app.js
npm init

Here we go

npm install express@4.16.3 --save
// Setup
var express = require('express');
var app = express();
// Listen
app.listen(3000, () => {
console.log('Server listing on 3000');
})
node app.js
npm install ejs --save
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get("/", (req, res) => {
res.render('index');
});
mkdir views
touch views/index.html
<html>
<head></head>
<body>
<h1>Node Blog</h1>
<form action="addpost" method="POST">
<textarea name="body"></textarea>
<input type="submit" value="Add Post" />
</form>
</body>
</html>
// Setup
var express = require('express');
var app = express();
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// Routes
app.get("/", (req, res) => {
res.render('index');
});
// Listen
app.listen(3000, () => {
console.log('Server listing on 3000');
})
brew install mongo
mkdir -p /data/db
sudo chown -R `id -un` /data/db
npm install mongoose --save
var mongoose = require('mongoose')
mongoose.connect("mongodb://localhost:27017/node-blog")
npm install body-parser
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true}))
var postSchema = new mongoose.Schema({ body: String });var Post = mongoose.model('Post', postSchema);
app.post('/addpost', (req, res) => {
var postData = new Post(req.body);
postData.save().then( result => {
res.redirect('/');
}).catch(err => {
res.status(400).send("Unable to save data");
});
});
app.get("/", (req, res) => {
Post.find({}, (err, posts) => {
res.render('index', { posts: posts})
});
});
<html>
<head></head>
<body>
<h1>Node Blog</h1>

<% for(let i = 0; i < posts.length; i++) { %>
<article>
<p><%= posts[i].body %></p>
</article>
<% } %>
<form action="addPost">
<textarea name="body"></textarea>
<input type="submit" value="Add Post" />
</form>
</body>
</html>
// Setup
var express = require('express');
var app = express();
var mongoose = require('mongoose')
mongoose.connect("mongodb://localhost:27017/node-blog")
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true}))
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
var postSchema = new mongoose.Schema({ body: String });var Post = mongoose.model('Post', postSchema);// Routes
app.get("/", (req, res) => {
Post.find({}, (err, posts) => {
res.render('index', { posts: posts})
});
});
app.post('/addpost', (req, res) => {
var postData = new Post(req.body);
postData.save().then( result => {
res.redirect('/');
}).catch(err => {
res.status(400).send("Unable to save data");
});
});
// Listen
app.listen(3000, () => {
console.log('Server listing on 3000');
})

If you liked this article, you’re going to LOVE my latest project. I’m building MySpace from scratch using Node + Nuxt. Follow along as I document the journey!

Programmer — Writer. @andyhartnett12

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store