This code creates a simple to-do list. The user enters a task, clicks "Add Task," and the task is added to the list.
2. Back-End (Server-Side): A Simple REST API with Node.js and Express
This example demonstrates a basic REST API endpoint using Node.js and Express. It doesn't interact with a database, but it shows the fundamental structure.
const express = require('express');
const app = express();
const port = 3000;
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello from the server!' });
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
This code creates a server that listens on port 3000. When a GET request is made to /api/hello, it returns a JSON object with a message. To run this, you'll need Node.js and npm (Node Package Manager) installed. Save the code as server.js, then run npm install express followed by node server.js.
3. Database Interaction (Conceptual):
Interacting with a database requires a database system (like MySQL, PostgreSQL, MongoDB) and a database driver (a library that allows your code to communicate with the database). Here's a conceptual example using a hypothetical database driver:
JavaScript (script.js):
This code creates a simple to-do list. The user enters a task, clicks "Add Task," and the task is added to the list.
2. Back-End (Server-Side): A Simple REST API with Node.js and Express
This example demonstrates a basic REST API endpoint using Node.js and Express. It doesn't interact with a database, but it shows the fundamental structure.
This code creates a server that listens on port 3000. When a GET request is made to
/api/hello
, it returns a JSON object with a message. To run this, you'll need Node.js and npm (Node Package Manager) installed. Save the code asserver.js
, then runnpm install express
followed bynode server.js
.3. Database Interaction (Conceptual):
Interacting with a database requires a database system (like MySQL, PostgreSQL, MongoDB) and a database driver (a library that allows your code to communicate with the database). Here's a conceptual example using a hypothetical database driver:
This code fetches all tasks