1+ const express = require ( 'express' ) ;
2+ const cors = require ( 'cors' ) ;
3+ const path = require ( 'path' ) ;
4+
5+ const app = express ( ) ;
6+ const PORT = process . env . PORT || 3000 ;
7+
8+ // Middleware
9+ app . use ( cors ( ) ) ;
10+ app . use ( express . json ( ) ) ;
11+ app . use ( express . urlencoded ( { extended : true } ) ) ;
12+
13+ // Root route for browsers (serves HTML)
14+ app . get ( '/' , ( req , res ) => {
15+ res . sendFile ( path . join ( __dirname , 'public' , 'index.html' ) ) ;
16+ } ) ;
17+
18+ app . get ( '/api/welcome' , ( req , res ) => {
19+ res . json ( {
20+ message : 'Welcome to AspireJavaScript Node.js App!' ,
21+ timestamp : new Date ( ) . toISOString ( ) ,
22+ version : '1.0.0'
23+ } ) ;
24+ } ) ;
25+
26+ app . get ( '/api/health' , ( req , res ) => {
27+ res . json ( {
28+ status : 'healthy' ,
29+ timestamp : new Date ( ) . toISOString ( )
30+ } ) ;
31+ } ) ;
32+
33+ app . get ( '/api/weather' , ( req , res ) => {
34+ // Mock weather data similar to other Aspire apps
35+ const weatherData = [
36+ {
37+ date : new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ,
38+ temperatureC : Math . floor ( Math . random ( ) * 35 ) - 5 ,
39+ summary : 'Sunny'
40+ } ,
41+ {
42+ date : new Date ( Date . now ( ) + 86400000 ) . toISOString ( ) . split ( 'T' ) [ 0 ] ,
43+ temperatureC : Math . floor ( Math . random ( ) * 35 ) - 5 ,
44+ summary : 'Cloudy'
45+ } ,
46+ {
47+ date : new Date ( Date . now ( ) + 172800000 ) . toISOString ( ) . split ( 'T' ) [ 0 ] ,
48+ temperatureC : Math . floor ( Math . random ( ) * 35 ) - 5 ,
49+ summary : 'Rainy'
50+ }
51+ ] . map ( item => ( {
52+ ...item ,
53+ temperatureF : Math . round ( ( item . temperatureC * 9 / 5 ) + 32 )
54+ } ) ) ;
55+
56+ res . json ( weatherData ) ;
57+ } ) ;
58+
59+ // Serve remaining static files from public directory (after API routes)
60+ app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
61+
62+ // Error handling middleware
63+ app . use ( ( err , req , res , next ) => {
64+ console . error ( err . stack ) ;
65+ res . status ( 500 ) . json ( {
66+ message : 'Something went wrong!' ,
67+ error : process . env . NODE_ENV === 'production' ? { } : err . stack
68+ } ) ;
69+ } ) ;
70+
71+ // General 404 handler (for non-API routes)
72+ app . use ( '*' , ( req , res ) => {
73+ res . status ( 404 ) . json ( {
74+ message : 'Route not found' ,
75+ path : req . originalUrl
76+ } ) ;
77+ } ) ;
78+
79+ // Start server
80+ app . listen ( PORT , ( ) => {
81+ console . log ( `Server is running on port ${ PORT } ` ) ;
82+ console . log ( `Environment: ${ process . env . NODE_ENV || 'development' } ` ) ;
83+ console . log ( `Visit: http://localhost:${ PORT } ` ) ;
84+ } ) ;
85+
86+ module . exports = app ;
0 commit comments