-
Notifications
You must be signed in to change notification settings - Fork 64
/
server.js
129 lines (104 loc) · 3.07 KB
/
server.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const express = require( 'express' );
const cors = require( 'cors' );
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
const config = require( './config' );
const axios = require( 'axios' );
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackConfig = require('./webpack.config');
const path = require('path')
const wordPressRestUrl = `${config.wordPressUrl}/${config.wordPressRestNameSpace}`;
console.log(wordPressRestUrl, 'wordpressurl')
const app = express();
app.use( cors() );
// Setting Paths
app.use('/static', express.static(path.join(__dirname, '/')));
// Setting views of the app
app.set('views', path.join(__dirname, './'));
app.set('view engine', 'pug');
// Parse application/x-www-form-urlencoded
app.use( bodyParser.urlencoded( { extended: false } ) );
// Parse application/json
app.use(bodyParser.json());
// Adding webpack build
// Middleware of webpack
if (process.env.NODE_ENV === 'development') {
console.log('in webpack hot middleware');
const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
}));
}
/**
* Sign in user
*
* @route http://localhost:5000/sign-in
*/
app.post( '/sign-in', ( req, res ) => {
jwt.sign( req.body ,config.tokenSecret , { expiresIn: 3600 }, ( err, token ) => {
if ( ! token ) {
res.json({ success: false, error: 'Token could not be generated' });
} else {
// Make a login request.
axios.post( `${wordPressRestUrl}/user/login`, req.body )
.then( response => {
res.json( {
success: true,
status: 200,
token,
userData: response.data.user.data
} );
} )
.catch( err => {
const responseReceived = err.response.data;
res.status(404).json({ success: false, status: 400, errorMessage: responseReceived.message });
} );
}
} );
} );
/**
* Create a new post
*
* @route http://localhost:5000/create-post
*/
app.post( '/create-post', ( req, res ) => {
jwt.verify(req.body.token, config.tokenSecret, function ( err, decoded ) {
if ( undefined !== decoded ) {
const postData = {
user_id: req.body.userID,
title: req.body.title,
content: req.body.content,
};
// Make a create post request.
axios.post( `${wordPressRestUrl}/post/create`, postData )
.then( response => {
res.json( {
success: true,
status: 200,
userData: response.data
} );
} )
.catch( err => {
const responseReceived = err.response.data;
res.status(404).json({ success: false, status: 400, errorMessage: responseReceived.message });
} );
} else {
res.status( 400 ).json( { success: false, status: 400, errorMessage: 'Authorization failed'} );
}
});
} );
app.get('/', (req, res) => {
res.render('index')
});
app.get('/login', (req, res) => {
res.render('index')
});
app.get('/dashboard/:userName', (req, res) => {
res.render('index')
});
app.get('/post/:id', (req, res) => {
res.render('index')
});
app.listen( process.env.PORT || 5000, () => console.log( 'Listening on port 5000' ) );