@@ -11,6 +11,7 @@ const {
1111 check,
1212 validationResult
1313} = require ( 'express-validator' ) ;
14+ const basicAuth = require ( 'express-basic-auth' ) ;
1415
1516const app = express ( ) ;
1617const port = 3005 ;
@@ -64,7 +65,7 @@ app.post('/post', csrfProtection, [
6465 } ) ,
6566 check ( 'description' , 'description length should be 100 to 140 characters Good for SEO' )
6667 . isLength ( {
67- min : 100 ,
68+ min : 60 ,
6869 max : 155
6970 } ) ,
7071 check ( 'postcontent' , 'Fill Some Post Content' ) . not ( ) . isEmpty ( ) . trim ( ) . escape ( ) ,
@@ -120,6 +121,74 @@ app.post('/post', csrfProtection, [
120121 }
121122} ) ;
122123
124+ app . get ( '/api' , basicAuth ( {
125+ users : { 'admin' :sitedata . password } ,
126+ challenge : true ,
127+ unauthorizedResponse : 'not authorized'
128+ } ) , [
129+ check ( 'title' , 'title length should be 50 to 60 characters Good for SEO' )
130+ . isLength ( {
131+ min : 10 ,
132+ max : 65
133+ } ) ,
134+ check ( 'description' , 'description length should be 100 to 140 characters Good for SEO' )
135+ . isLength ( {
136+ min : 60 ,
137+ max : 155
138+ } ) ,
139+ check ( 'postcontent' , 'Fill Some Post Content' ) . not ( ) . isEmpty ( ) . trim ( ) . escape ( ) ,
140+ check ( 'tag' , 'Enter Atleast one Tag for Post' ) . not ( ) . isEmpty ( ) . trim ( ) . escape ( ) ,
141+ ] , function ( req , res ) {
142+
143+ res . header ( 'X-Frame-Options' , 'DENY' ) ;
144+ res . header ( 'X-XSS-Protection' , '1; mode=block' ) ;
145+ res . header ( 'X-Content-Type-Options' , 'nosniff' ) ;
146+ res . header ( 'Strict-Transport-Security' , 'max-age=63072000' ) ;
147+
148+ const blog_title = req . query . title
149+ const random_id = Math . floor ( 1000 + Math . random ( ) * 9000 )
150+ const basename = sitedata . url_data + "-" + random_id
151+
152+ const errors = validationResult ( req ) ;
153+
154+ if ( ! errors . isEmpty ( ) ) {
155+ res . status ( 400 ) . json ( errors ) ;
156+ } else {
157+
158+ const seo_url = slugify ( blog_title , {
159+ replacement : '-' ,
160+ remove : / [ * + ~ . ( ) ' " ! : @ ] / g,
161+ lower : true ,
162+ strict : false
163+ } ) ;
164+
165+ var title = blog_title ;
166+ var description = req . query . description ;
167+ var date = formattedDate ;
168+ var tag = req . query . tag ;
169+ var postcontent = req . query . postcontent ;
170+ let content = [ {
171+ title : title || "Example Post title" ,
172+ description : description || "Example Post description" ,
173+ date : date ,
174+ tag : tag || "Hello World" ,
175+ postcontent : postcontent || "Example Post Content" ,
176+ slug : decodeURIComponent ( seo_url )
177+ } ] ;
178+ let template = fs . readFileSync ( "./template.md" ) . toString ( )
179+ content . forEach ( post_data => {
180+ let output = render ( template , post_data )
181+ const clean_url = basename ;
182+ fs . writeFileSync ( `${ sitedata . storage_path } /${ clean_url } .${ sitedata . format } ` , output )
183+ console . log ( post_data ) ;
184+ } )
185+ res . status ( 200 ) . json ( {
186+ sucess : 1 ,
187+ message : 'Post Created'
188+ } ) ;
189+ }
190+ } ) ;
191+
123192app . use ( '/' , function ( req , res ) {
124193 res . status ( 404 ) . json ( {
125194 error : 1 ,
0 commit comments