Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
var weatherApi = builder.AddProject<Projects.AspireJavaScript_MinimalApi>("weatherapi")
.WithExternalHttpEndpoints();

builder.AddJavaScriptApp("angular", "../AspireJavaScript.Angular")
builder.AddJavaScriptApp("angular", "../AspireJavaScript.Angular", runScriptName: "start")
.WithReference(weatherApi)
.WaitFor(weatherApi)
.WithHttpEndpoint(env: "PORT")
.WithExternalHttpEndpoints()
.PublishAsDockerFile();

builder.AddJavaScriptApp("react", "../AspireJavaScript.React")
builder.AddJavaScriptApp("react", "../AspireJavaScript.React", runScriptName: "start")
.WithReference(weatherApi)
.WaitFor(weatherApi)
.WithEnvironment("BROWSER", "none") // Disable opening browser on npm start
Expand All @@ -19,6 +19,7 @@
.PublishAsDockerFile();

builder.AddJavaScriptApp("vue", "../AspireJavaScript.Vue")
.WithRunScript("start")
.WithNpm(installCommand: "ci") // Use 'npm ci' for clean install, requires lock file
.WithReference(weatherApi)
.WaitFor(weatherApi)
Expand All @@ -31,6 +32,12 @@
.WithEnvironment("BROWSER", "none")
.WithExternalHttpEndpoints();

builder.AddNodeApp("node", "../AspireJavaScript.NodeApp", "app.js")
.WithRunScript("dev") // Use 'npm run dev' for development
.WithHttpEndpoint(env: "PORT")
.WithExternalHttpEndpoints()
.PublishAsDockerFile();

weatherApi.PublishWithContainerFiles(reactvite, "./wwwroot");

builder.Build().Run();
86 changes: 86 additions & 0 deletions playground/AspireWithJavaScript/AspireJavaScript.NodeApp/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const express = require('express');
const cors = require('cors');
const path = require('path');

const app = express();
const PORT = process.env.PORT || 3000;

// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Root route for browsers (serves HTML)
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.get('/api/welcome', (req, res) => {
res.json({
message: 'Welcome to AspireJavaScript Node.js App!',
timestamp: new Date().toISOString(),
version: '1.0.0'
});
});

app.get('/api/health', (req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString()
});
});

app.get('/api/weather', (req, res) => {
// Mock weather data similar to other Aspire apps
const weatherData = [
{
date: new Date().toISOString().split('T')[0],
temperatureC: Math.floor(Math.random() * 35) - 5,
summary: 'Sunny'
},
{
date: new Date(Date.now() + 86400000).toISOString().split('T')[0],
temperatureC: Math.floor(Math.random() * 35) - 5,
summary: 'Cloudy'
},
{
date: new Date(Date.now() + 172800000).toISOString().split('T')[0],
temperatureC: Math.floor(Math.random() * 35) - 5,
summary: 'Rainy'
}
].map(item => ({
...item,
temperatureF: Math.round((item.temperatureC * 9/5) + 32)
}));

res.json(weatherData);
});

// Serve remaining static files from public directory (after API routes)
app.use(express.static(path.join(__dirname, 'public')));

// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
message: 'Something went wrong!',
error: process.env.NODE_ENV === 'production' ? {} : err.stack
});
});

// General 404 handler (for non-API routes)
app.use('*', (req, res) => {
res.status(404).json({
message: 'Route not found',
path: req.originalUrl
});
});

// Start server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`Visit: http://localhost:${PORT}`);
});

module.exports = app;
Loading