I have an existing node-express project running on express using express server and have middlewares and routing on express
var express = require('express');
var app = express();
require('/path/to/express_conf_file')(app);
app.listen(config.port);
I want to use json-server for easy mocking and I've followed this. My current code looks like this:
var express = require('express');
var app = require('json-server');
var middlewares = express();
var server = app.create(); // Returns an Express server
**var router = server.router('db.json');** // Returns an Express router
server.use(middlewares);
server.use(router);
require('./config/express')(middlewares, config);
server.listen(4000);
server.route('db.json') seems to be deprecated in Express 4.x. What needs to be done to use 'db.json' with express 4.x?
I am invoking my application and json-server using concurrenrly using npm start and in package.json I've defined:
"js-server": "json-server --watch db.json --port 4000",
"start": "concurrently \"gulp command\" \"npm run js-server\""
Can somebody please advice as what should be the correct way of using json-server with Express 4.x?
It is solved here. json-server can be mounted at a certain path within express server. No need to start the json-server separately now. Only gulp command can be used to launch express server.
The json server can be hosted separately also and can be accessed through the specified port from different appilication(s).
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 am running a vue application (VUE-CLI 3) on Node. In dev mode, I run 'npm run serve' and the application is brought up and works as expected.
I would now like to add websocket code to the server.
Most examples I see have some setup code similar to:
const http = require('http');
const server = http.createServer();
const wsServer = new WebsocketServer({httpServer: server});
When I run 'npm run serve' I now am greeted with the following error message:
'http.createServer is not a function'
Is there a way to attach the websockets to the current running node server when invoked via npm run serve? In other words, can I skip the createServer call and attach it to whatever is currently running?
Well, your code should read something like this...
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
You will need to use ExpressJS on the back-end to handle messages.
I am trying to deploy my angular application on windows server for that i am using the express server .
I have configured my server.js file like this:
var express = require('express');
var app = express();
const path = require('path');
app.use(express.static(__dirname + '/dist'));
app.listen(process.env.PORT || 4200);
app.get('*/', function(req,res){
res.sendFile(path.join(__dirname + '/dist/index.html'));
})
console.log('App is listing on ' + 4200);
Also, i have deployed the content that was produced using the ng build command inside dist.
I can successfully able to access the application when i enter http://localhost:4200 in my browser.
But, the problem what i am facing here is the proxy server is not getting created which was used to be created when i was using :
"start": "ng serve --proxy-config local-proxy.config.json",
But, now since i am not calling ng serve instead of this i am calling node server.js which is serving my application on server.
"start": "node server.js --proxy-config local-proxy.config.json",
Can someone can help me please.
I generated an Angular2 project by using Angular-CLI. This is the front-end side. Now I'd like to add an Express server but I don't know how exactly should I do that.
Should I add an express app.js file into generated by Angualar-CLI "src" directory? I've tried it and what's next? To start Angular-CLI project, I need to type ng serve in CMD. To start server, I need to type node app.js.
Example of app.js
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser');
var app = express();
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen("8080", function() {
console.log("at 8080 port");
});
module.exports = app;
Well... Angular2 doesn't work when I start app by node app.js. ng serve builds all files and in mysterious for me way puts it all together.
Maybe I should create front-end and back-end completely separated? But what then? Use technology like Nginx for reverse proxy?
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');
});