Setting Up an HTTP Server for Local Testing
Requirements:
- Node.js installed on your system
- Basic knowledge of the command line
Steps:
-
Create a Project Directory:
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your project.
-
Initialize a Node.js Project:
npm init -y
-
Install the
http-server
Package:npm install http-server --save-dev
-
Create Your JavaScript Files:
- Create an
extension.js
file, this will be your extensions code. - Example of what your extenion.js might look like:
- Create an
(function(Scratch) {
'use strict';
class Extension {
getInfo() {
return {
id: "johnMyExtension",
name: "My Extension",
blocks: [
{
opcode: 'logToConsole',
text: 'log to console',
blockType: Scratch.BlockType.COMMAND
},
{
opcode: 'testReporter',
text: 'testing!',
blockType: Scratch.BlockType.REPORTER,
disableMonitor: true,
allowDropAnywhere: true
}
]
};
}
logToConsole() {
console.log('Hello world!');
}
testReporter() {
return "Hello world!";
}
}
Scratch.extensions.register(new Extension());
})(Scratch);
-
Start the HTTP Server:
- In your terminal, navigate to your project directory.
- Run the following command to start the server:
npx http-server
This command starts the server and serves files from the current directory.
-
Access Your Local Server:
- Open a web browser.
- Go to
http://localhost:8080/extension.js
orhttp://127.0.0.1:8080/extension.js
in the address bar. - You should see your
extenion.js
file rendered in the browser.
-
Testing:
- Make any changes to your JavaScript files.
- Refresh the browser to see the changes instantly.
-
Opening in Snail IDE:
- Load
http://localhost:8080/extension.js
orhttp://127.0.0.1:8080/extension.js
in the custom extension menu,
- Load
-
Stopping the Server:
- To stop the server, go back to the terminal where the server is running.
- Press
Ctrl + C
to stop the server.
Additional Notes:
- By default, the
http-server
package serves files on port8080
. - You can specify a different port using the
-p
or--port
option followed by the desired port number when starting the server, likenpx http-server -p 3000
. - Remember, this setup is intended for local testing only and should not be used for production purposes.