I have installed meteor library.
I want to run an angular project which has no back-end (of static content).
I want to create a server file using node.js for static content.
Is it possible to to create that and execute ?
There is a very simple example of how to create a static server in Node.js that server static content pages,
the following code is in the myserver.js file:
var http = require('http');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic("./");
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
server.listen(8000)
You need to install through NPM from command line:
$ npm install finalhandler serve-static
$ node myserver.js
It's possible with Meteor, but it's an overkill.
remove the default mongo package ($ meteor remove mongo)
put all your static files into a folder called public
You're done. This way your production build won't require a MongoDB server.
But it's a lot easier to just use the http-server NPM package to setup a static file server with Node.js.
Related
I have an Angular 6 application and an existing nodejs api application.
So far I have used
ng serve
to run and build the angular application.
I now want to serve my angular application from the existing node js server.
How do I do that ? I can't find any documentation.
Steps:
Do ng build, It will create a dist folder which you can easily serve from node webserver framework like express, Hapi or Koa
if you are using express.js you can server angular app.use(express.static(path.join(__dirname, 'dist')));
Now use node server URL to serve angular like http://localhost:nodeport
If you are using Hapi: check this out https://hapi.dev/tutorials/servingfiles/?lang=en_US
================================basic express server================
const express = require('express');
const app = express();
const path = require("path");
const fs = require("fs");
//const bodyParser = require('body-parser');
app.use(express.static(path.join(__dirname, 'dist')));
//app.use(bodyParser.json());
//app.use('/api/v1/', require('./api/routes'));
app.listen(8080,function(err){
if(!err){
console.log("server is running at port:8080);
}
})
You have two ways of serving an Angular SPA:
Usually dev: the Webpack-run server, which is ng serve. Dynamic in the sense that any modification to a file starts a rebuild and updates the output.
Usually prod: you build all the html/js files (with ng build [...]) for the SPA to be statically served by a node server.
In your case, if you'd like to use an existing node server, it means you'll have to build the files (with ng build) and then hook up the usual node static files serving snippet in your node app.
Beware though: you'll have to do a full build each time you want to update the display. So it's ok if it's not that often, but not ok for a dev environment I guess.
I have this socket server:
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
...
app.listen(3001);
Express and socket versions:
"express": "^4.16.3",
"socket.io": "^2.1.1"
And I call this on client side:
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
When I run socket server and open client app on chrome, I get this message: "Failed to load resource: the server responded with a status of 404 (Not Found)".
I don't intent to copy socket.io.js to a public folder as it's not the right way to do it according to socket.io docs.
That said, what am I doing wrong?
Possible fixes:
Make sure you run npm install and you don't have errors or incomplete installations:
inside your project's directory, go to ./node_modules/socket.io/node_modules/socket.io-client/dist and copy the file named socket.io.js and put it into one of your static or resources directory.
Don't forget put the following line to serve static files, now you should be able to browse it under: http://localhost:3000/resources/socket.io/socket.io.js
app.use('/resources', express.static('resources'));
If not fixed, here is a complete basic example follow the steps below.
Copy paste the following into a file myfile.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
Run the following commands:
$ npm i express --save
$ npm i socket.io --save
$ node myfile.js
Open your browser http://localhost:3000/socket.io/socket.io.js
Can we import all node.js modules in webpack and create a bundle.js? Example what if I use http module and webpack application and bundle it and run in browser?
main.js
var http = require('http');
var server = http.createServer(function (req, res) {
// send output
});
server.listen(8080);
When using Node's built-in libraries with Webpack, it will automatically import a browser-compatible version when available.
You can see the entire list and matching packages in this file.
For http, you'll end up with http-browserify instead. Not everything is supported, so creating a HTTP server will not work (as this isn't possible in a browser). You can still use http to do requests as shown in the documentation.
I am trying to learn the basics of node.js and socket.io. I have been using this tutorial http://tutorialzine.com/2012/08/nodejs-drawing-game/
the full code for this problem can be seen in the link above.
I can create a basic web server with node.js and get it to return hello world so I am sure that's installed correctly. However upon installing these packages
npm install socket.io#0.9.10 node-static
and setting up the serverside js as instructed
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
var fileServer = new nstatic.Server('./');
app.listen(8080);
I just get this prompt in my cmd and a constantly hanging web browser, instead of the html page that is meant to be served.I think I may have messed up an install but upon looking at the list of installed packages in npm it states both socket.io and node-static are present.
The code below should be more effective?, it looks like you are missing the handler part. The response must be explicitly ended or browser requests will hang forever like you are seeing. The node-static file.serve method manages the request once you pass it down. The source for .serve is here: https://github.com/cloudhead/node-static/blob/master/lib/node-static.js#L164
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
app.listen(8080);
var file = new nstatic.Server('./');
function handler(request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
}
console.log('started')
Note also that the default file to serve to responses at / is index.html.
I downloaded express today from npm, and to my surprise, it gave me express 4.0.0 instead of express 3.x.x.
I'm trying to configure a basic server with connect logger and body parser, but I'm not having much luck.
Could someone provide me with a boilerplate app.js file using express 4.0?
Thanks!
Got it!
You can get a skeleton app using:
$ npm install -g express-generator
Alternatively, you can swap out the connect logger and body parser for their connect standalone siblings (and it might be useful for learning what each middleware does, rather than relying on the generator to throw together a bunch of dependencies you may not need):
(based on Express 3.x to 4.x Migration guide)
var express = require('express');
var server = express();
...
server.use(require('body-parser')); //previously bodyparser (or rather urlencoded/json)
server.use(require('morgan')()); //previously connect-logger
...
server.listen('3000', function() {
console.log('server started');
});