Open barrycumbie opened 2 years ago
example 'backend/app.js' for express endpoints
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
//app.use(bodyParser.urlencoded({extended: false}));
//don't need but shows how to do URL
// app.use((req, res, next) => {
// console.log('first middleware');
// next();
// });
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PATCH, DELETE, OPTIONS'
);
next();
});
app.post('/api/posts', (req, res, next) => {
//...no db yet, can't store persist
const post = req.body;
console.log(post);
res.status(201).json({
message: 'Post added successfully'
});
//201 = okay + new resource added
});
app.get('/api/posts', (req, res, next) => {
// res.send('hello from express');
posts = [{
id: 'fsae08ads',
title: 'first server-side post',
content: 'This is coming from the server'
},
{
id: 'fsae08ads',
title: 'second server-side post',
content: 'This is coming from the server!!'
}
]
res.status(200).json({
message: 'Posts fectched successfully',
posts: posts
});
});
module.exports = app;
npm install --save body-parser node server.js npm run start:server npm install --save-dev nodemon npm install --save express express -version npm list express
Description
level up 🆙 our current
ng
app to include theE
A
&N
of the mean stack. That is:pre-reqs
ng -g -c component-name
I thinkco-req
here's an template for the node.js server: https://github.com/barrycumbie/animated-octo-adventure/wiki/code-template:-node-server.js