1- const express = require ( ' express' ) ;
2- const cors = require ( ' cors' ) ;
3- const helmet = require ( ' helmet' ) ;
4- require ( ' dotenv' ) . config ( ) ;
1+ const express = require ( " express" ) ;
2+ const cors = require ( " cors" ) ;
3+ const helmet = require ( " helmet" ) ;
4+ require ( " dotenv" ) . config ( ) ;
55
66const app = express ( ) ;
77const port = process . env . PORT || 3000 ;
88
99// Middleware
1010app . use ( helmet ( ) ) ;
11- app . use ( cors ( {
12- origin : [ 'http://localhost:3000' , 'http://127.0.0.1:3000' ]
13- } ) ) ;
11+ app . use (
12+ cors ( {
13+ origin : [ "http://localhost:3000" , "http://127.0.0.1:3000" ] ,
14+ } ) ,
15+ ) ;
1416app . use ( express . json ( ) ) ;
1517app . use ( express . urlencoded ( { extended : true } ) ) ;
1618
1719// Routes
18- app . get ( '/' , ( req , res ) => {
19- res . json ( { message : ' Node.js backend API is running' } ) ;
20+ app . get ( "/" , ( req , res ) => {
21+ res . json ( { message : " Node.js backend API is running" } ) ;
2022} ) ;
2123
22- app . get ( ' /api/health' , ( req , res ) => {
23- res . json ( { status : ' healthy' , timestamp : new Date ( ) . toISOString ( ) } ) ;
24+ app . get ( " /api/health" , ( req , res ) => {
25+ res . json ( { status : " healthy" , timestamp : new Date ( ) . toISOString ( ) } ) ;
2426} ) ;
2527
2628// API routes
27- app . get ( ' /api/items' , ( req , res ) => {
29+ app . get ( " /api/items" , ( req , res ) => {
2830 // Mock data - replace with database queries
2931 const items = [
30- { id : 1 , name : 'Sample Item 1' , description : 'A sample item' , price : 29.99 } ,
31- { id : 2 , name : 'Sample Item 2' , description : 'Another sample item' , price : 49.99 }
32+ {
33+ id : 1 ,
34+ name : "Sample Item 1" ,
35+ description : "A sample item" ,
36+ price : 29.99 ,
37+ } ,
38+ {
39+ id : 2 ,
40+ name : "Sample Item 2" ,
41+ description : "Another sample item" ,
42+ price : 49.99 ,
43+ } ,
3244 ] ;
3345 res . json ( items ) ;
3446} ) ;
3547
36- app . post ( ' /api/items' , ( req , res ) => {
48+ app . post ( " /api/items" , ( req , res ) => {
3749 const { name, description, price } = req . body ;
3850
3951 // Basic validation
4052 if ( ! name || ! price ) {
41- return res . status ( 400 ) . json ( { error : ' Name and price are required' } ) ;
53+ return res . status ( 400 ) . json ( { error : " Name and price are required" } ) ;
4254 }
4355
4456 // Mock response - replace with database insertion
4557 const newItem = {
4658 id : Date . now ( ) , // Simple ID generation
4759 name,
4860 description,
49- price : parseFloat ( price )
61+ price : parseFloat ( price ) ,
5062 } ;
5163
5264 res . status ( 201 ) . json ( newItem ) ;
@@ -55,16 +67,16 @@ app.post('/api/items', (req, res) => {
5567// Error handling middleware
5668app . use ( ( err , req , res , next ) => {
5769 console . error ( err . stack ) ;
58- res . status ( 500 ) . json ( { error : ' Something went wrong!' } ) ;
70+ res . status ( 500 ) . json ( { error : " Something went wrong!" } ) ;
5971} ) ;
6072
6173// 404 handler
6274app . use ( ( req , res ) => {
63- res . status ( 404 ) . json ( { error : ' Route not found' } ) ;
75+ res . status ( 404 ) . json ( { error : " Route not found" } ) ;
6476} ) ;
6577
6678app . listen ( port , ( ) => {
67- console . log ( \ `Server running on http :/ / localhost :\ ${ port} \ `) ;
79+ console . log ( `Server running on http://localhost:${ port } ` ) ;
6880} ) ;
6981
70- module . exports = app ;
82+ module . exports = app ;
0 commit comments